Bird
Raised Fist0
SQLquery~15 mins

COUNT function behavior in SQL - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - COUNT function behavior
What is it?
The COUNT function in SQL is used to count the number of rows that match a certain condition or simply count all rows in a table. It helps you find out how many records exist or meet specific criteria. COUNT can count all rows, only non-null values in a column, or distinct values depending on how you use it.
Why it matters
Without COUNT, it would be hard to quickly know how many records exist or satisfy a condition in a database. This function helps businesses and applications summarize data, like counting customers, orders, or errors. Without it, you would have to manually check each row, which is slow and error-prone.
Where it fits
Before learning COUNT, you should understand basic SQL SELECT queries and how tables store data. After mastering COUNT, you can learn other aggregate functions like SUM, AVG, MIN, and MAX, and how to group data with GROUP BY.
Mental Model
Core Idea
COUNT tells you how many rows or values exist that meet your criteria in a table.
Think of it like...
COUNT is like counting how many apples are in a basket, but you can choose to count only red apples, all apples, or only unique apples.
┌───────────────┐
│   Table Rows  │
├───────────────┤
│ Row 1         │
│ Row 2         │
│ Row 3         │
│ ...           │
└───────────────┘
       ↓
┌─────────────────────────────┐
│ COUNT(*) counts all rows     │
│ COUNT(column) counts non-null│
│ COUNT(DISTINCT column) counts│
│ unique non-null values       │
└─────────────────────────────┘
Build-Up - 7 Steps
1
FoundationBasic COUNT usage with all rows
🤔
Concept: COUNT(*) counts every row in a table, including rows with nulls.
SELECT COUNT(*) FROM table_name; This query returns the total number of rows in the table, no matter what values the rows contain.
Result
A single number showing total rows in the table.
Understanding COUNT(*) as a total row counter helps you quickly know the size of your data.
2
FoundationCOUNT with a specific column counts non-null values
🤔
Concept: COUNT(column_name) counts only rows where that column is not null.
SELECT COUNT(column_name) FROM table_name; This counts how many rows have a value in that column, ignoring rows where the column is null.
Result
A number showing how many rows have a non-null value in the specified column.
Knowing COUNT(column) ignores nulls helps you measure how many meaningful entries exist in that column.
3
IntermediateUsing COUNT with DISTINCT for unique values
🤔Before reading on: do you think COUNT(DISTINCT column) counts all rows or only unique values? Commit to your answer.
Concept: COUNT(DISTINCT column) counts the number of unique non-null values in a column.
SELECT COUNT(DISTINCT column_name) FROM table_name; This query returns how many different values appear in that column, ignoring duplicates and nulls.
Result
A number showing the count of unique values in the column.
Understanding COUNT(DISTINCT) helps you find diversity or variety in your data, like how many different customers made orders.
4
IntermediateCOUNT behavior with WHERE filters
🤔Before reading on: does COUNT count rows before or after applying WHERE filters? Commit to your answer.
Concept: COUNT counts rows only after the WHERE condition filters the data.
SELECT COUNT(*) FROM table_name WHERE condition; Only rows meeting the condition are counted.
Result
A number showing how many rows satisfy the condition.
Knowing WHERE filters data before counting lets you focus on subsets, like counting only active users.
5
IntermediateCOUNT in GROUP BY queries
🤔Before reading on: does COUNT count rows per group or total rows in the table? Commit to your answer.
Concept: COUNT counts rows within each group when used with GROUP BY.
SELECT column, COUNT(*) FROM table_name GROUP BY column; This counts how many rows exist for each distinct value in the column.
Result
A list of groups with their respective counts.
Understanding COUNT with GROUP BY helps summarize data by categories, like counting orders per product.
6
AdvancedCOUNT with NULLs and why it matters
🤔Before reading on: does COUNT(column) count rows where the column is NULL? Commit to your answer.
Concept: COUNT(column) skips NULL values, which can cause counts to be less than total rows.
If a column has NULLs, COUNT(column) ignores those rows. Example: Row 1: value Row 2: NULL Row 3: value COUNT(column) returns 2, not 3.
Result
A count excluding rows with NULL in that column.
Knowing how NULLs affect COUNT prevents mistakes when interpreting counts, especially with incomplete data.
7
ExpertCOUNT optimization and execution surprises
🤔Before reading on: do you think COUNT(*) always scans all rows physically? Commit to your answer.
Concept: Database engines optimize COUNT(*) differently; sometimes they use indexes or metadata to speed counting without scanning all rows.
Some databases keep row counts in metadata or use index-only scans for COUNT(*). However, COUNT(column) or COUNT(DISTINCT column) usually requires scanning data. This affects performance and query planning.
Result
Faster or slower query execution depending on COUNT usage and database engine.
Understanding how COUNT is optimized helps write efficient queries and avoid performance pitfalls.
Under the Hood
COUNT works by scanning rows in a table or index and incrementing a counter for each row that meets the criteria. COUNT(*) counts every row, while COUNT(column) checks if the column value is not null before counting. COUNT(DISTINCT column) collects unique values in a temporary structure to avoid duplicates. Database engines may optimize COUNT(*) by using metadata or indexes to avoid full scans.
Why designed this way?
COUNT was designed to provide quick summary information about data size and content. Counting all rows or non-null values separately allows flexibility for different needs. Using DISTINCT inside COUNT helps analyze uniqueness. Optimizations evolved to improve performance on large datasets, balancing accuracy and speed.
┌───────────────┐
│   Table Scan  │
├───────────────┤
│ For each row: │
│  ├─ Check WHERE│
│  ├─ If passes: │
│  │   ├─ If *:  │
│  │   │  count++│
│  │   ├─ If col:│
│  │   │  if not │
│  │   │  null:  │
│  │   │  count++│
│  │   ├─ If DISTINCT:│
│  │   │  add to set│
│  │   │  count = set size│
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does COUNT(column) count rows where the column is NULL? Commit to yes or no.
Common Belief:COUNT(column) counts all rows including those with NULL values in that column.
Tap to reveal reality
Reality:COUNT(column) counts only rows where the column is NOT NULL; NULLs are ignored.
Why it matters:Misunderstanding this leads to undercounting data and wrong conclusions about data completeness.
Quick: Does COUNT(*) count only distinct rows? Commit to yes or no.
Common Belief:COUNT(*) counts only unique rows, ignoring duplicates.
Tap to reveal reality
Reality:COUNT(*) counts every row, including duplicates; it does not remove duplicates.
Why it matters:Assuming COUNT(*) removes duplicates can cause wrong data summaries and misinterpretation.
Quick: Does COUNT(DISTINCT column) include NULL values in its count? Commit to yes or no.
Common Belief:COUNT(DISTINCT column) counts NULL as a distinct value.
Tap to reveal reality
Reality:COUNT(DISTINCT column) ignores NULL values; they are not counted as distinct.
Why it matters:Counting NULL as distinct inflates unique counts and misrepresents data diversity.
Quick: Does COUNT(*) always scan every row physically? Commit to yes or no.
Common Belief:COUNT(*) always scans all rows in the table physically to count them.
Tap to reveal reality
Reality:Some databases optimize COUNT(*) using metadata or indexes to avoid full scans.
Why it matters:Not knowing this can lead to wrong assumptions about query performance and optimization.
Expert Zone
1
COUNT(*) can be optimized by the database engine using metadata or index-only scans, but COUNT(column) usually requires scanning actual data.
2
COUNT(DISTINCT) can be expensive on large datasets because it needs to track unique values, often using temporary memory or disk.
3
NULL values are never counted by COUNT(column) or COUNT(DISTINCT column), which can cause subtle bugs if NULLs represent meaningful missing data.
When NOT to use
COUNT is not suitable when you need to count complex conditions involving multiple columns or need approximate counts on huge datasets. Alternatives include using filtered aggregates, approximate count functions like HyperLogLog, or specialized analytics tools.
Production Patterns
In production, COUNT(*) is often used for quick row counts, while COUNT(column) helps check data completeness. COUNT(DISTINCT) is used for unique user counts or distinct events. Optimizing queries by avoiding COUNT(DISTINCT) on large columns or using indexed columns is common to improve performance.
Connections
Set Theory
COUNT(DISTINCT) relates to counting unique elements in a set.
Understanding how COUNT(DISTINCT) works is like understanding how sets contain unique elements, which helps grasp uniqueness in data.
Statistics - Frequency Counting
COUNT is a basic frequency counting operation in statistics.
Knowing COUNT is like tallying occurrences helps connect database queries to statistical data analysis.
Inventory Management
COUNT is similar to counting items in stock or sales in inventory systems.
Relating COUNT to real-world inventory counting clarifies its practical use in business.
Common Pitfalls
#1Counting rows with COUNT(column) expecting to include NULLs.
Wrong approach:SELECT COUNT(column_name) FROM table_name;
Correct approach:SELECT COUNT(*) FROM table_name;
Root cause:Misunderstanding that COUNT(column) excludes NULL values, leading to undercounting.
#2Using COUNT(DISTINCT column) on large datasets without considering performance.
Wrong approach:SELECT COUNT(DISTINCT large_column) FROM big_table;
Correct approach:Use approximate distinct count functions or pre-aggregated data for large datasets.
Root cause:Not realizing COUNT(DISTINCT) can be slow and resource-heavy on big data.
#3Assuming COUNT(*) removes duplicate rows.
Wrong approach:SELECT COUNT(*) FROM table_name WHERE duplicates_exist;
Correct approach:SELECT COUNT(DISTINCT column_name) FROM table_name;
Root cause:Confusing COUNT(*) with COUNT(DISTINCT), leading to incorrect data summaries.
Key Takeaways
COUNT(*) counts all rows in a table, including those with NULL values.
COUNT(column) counts only rows where the specified column is not NULL.
COUNT(DISTINCT column) counts unique non-null values, ignoring duplicates and NULLs.
WHERE filters apply before COUNT, so only filtered rows are counted.
Database engines optimize COUNT(*) differently, affecting query performance.

Practice

(1/5)
1. What does the SQL function COUNT(*) do when used in a query?
easy
A. Counts only rows with NULL values
B. Counts only rows where all columns are NOT NULL
C. Counts all rows in the table, including those with NULL values
D. Counts only distinct values in a column

Solution

  1. Step 1: Understand COUNT(*) behavior

    The COUNT(*) function counts every row in the table regardless of NULL values in any column.
  2. Step 2: Compare with other COUNT variants

    Unlike COUNT(column), which skips NULLs, COUNT(*) includes all rows.
  3. Final Answer:

    Counts all rows in the table, including those with NULL values -> Option C
  4. Quick Check:

    COUNT(*) counts all rows [OK]
Hint: COUNT(*) counts every row, NULL or not [OK]
Common Mistakes:
  • Thinking COUNT(*) skips NULL rows
  • Confusing COUNT(*) with COUNT(column)
  • Assuming COUNT(*) counts distinct values
2. Which of the following is the correct syntax to count only non-NULL values in the column age from the table persons?
easy
A. SELECT COUNT(*) FROM persons;
B. SELECT COUNT(age) FROM persons;
C. SELECT COUNT(DISTINCT age) FROM persons;
D. SELECT COUNT(age IS NOT NULL) FROM persons;

Solution

  1. Step 1: Identify how to count non-NULL values

    COUNT(column) counts only non-NULL values in that column.
  2. Step 2: Check syntax correctness

    SELECT COUNT(age) FROM persons; correctly counts non-NULL age values.
  3. Final Answer:

    SELECT COUNT(age) FROM persons; -> Option B
  4. Quick Check:

    COUNT(column) counts non-NULL values [OK]
Hint: Use COUNT(column) to count non-NULL values [OK]
Common Mistakes:
  • Using COUNT(*) to count non-NULL values
  • Using WHERE clause unnecessarily
  • Using COUNT with boolean expressions
3. Given the table employees with the column department containing values: ['HR', 'IT', NULL, 'IT', 'HR', 'Finance', NULL], what is the result of the query SELECT COUNT(DISTINCT department) FROM employees;?
medium
A. 3
B. 5
C. 7
D. 4

Solution

  1. Step 1: Identify distinct non-NULL values in department

    The distinct non-NULL values are 'HR', 'IT', and 'Finance'. That's 3 unique values.
  2. Step 2: Understand COUNT(DISTINCT) behavior

    COUNT(DISTINCT column) counts unique non-NULL values only, so NULLs are excluded.
  3. Step 3: Count distinct values

    There are 3 distinct non-NULL values, so the count is 3.
  4. Final Answer:

    3 -> Option A
  5. Quick Check:

    COUNT(DISTINCT) excludes NULLs [OK]
Hint: COUNT(DISTINCT) counts unique non-NULL values only [OK]
Common Mistakes:
  • Including NULL as a distinct value
  • Counting total rows instead of distinct
  • Confusing COUNT(*) with COUNT(DISTINCT)
4. Consider the query: SELECT COUNT(employee_id) FROM staff; but it returns 0 even though the table has rows. What is the most likely reason?
medium
A. The column employee_id contains only NULL values
B. The table staff is empty
C. COUNT(employee_id) counts all rows including NULLs
D. Syntax error in the query

Solution

  1. Step 1: Understand COUNT(column) behavior

    COUNT(column) counts only non-NULL values in that column.
  2. Step 2: Analyze why count is zero

    If all employee_id values are NULL, COUNT returns 0 even if rows exist.
  3. Final Answer:

    The column employee_id contains only NULL values -> Option A
  4. Quick Check:

    COUNT(column) excludes NULLs [OK]
Hint: COUNT(column) returns 0 if all values are NULL [OK]
Common Mistakes:
  • Assuming COUNT(column) counts all rows
  • Thinking table is empty without checking data
  • Assuming syntax error without checking query
5. You want to find how many unique customers placed orders, but some orders have NULL customer IDs. Which query correctly counts unique customers excluding NULLs?
hard
A. SELECT COUNT(customer_id) FROM orders;
B. SELECT COUNT(*) FROM orders WHERE customer_id IS NOT NULL;
C. SELECT COUNT(DISTINCT *) FROM orders;
D. SELECT COUNT(DISTINCT customer_id) FROM orders;

Solution

  1. Step 1: Understand requirement for unique customers excluding NULLs

    We need to count distinct customer IDs ignoring NULLs.
  2. Step 2: Analyze options

    SELECT COUNT(DISTINCT customer_id) FROM orders; uses COUNT(DISTINCT customer_id), which counts unique non-NULL values correctly.
  3. Step 3: Eliminate incorrect options

    SELECT COUNT(customer_id) FROM orders; counts all non-NULL customer IDs including duplicates. SELECT COUNT(*) FROM orders WHERE customer_id IS NOT NULL; counts rows with non-NULL customer IDs but not distinct. SELECT COUNT(DISTINCT *) FROM orders; is invalid syntax.
  4. Final Answer:

    SELECT COUNT(DISTINCT customer_id) FROM orders; -> Option D
  5. Quick Check:

    COUNT(DISTINCT column) counts unique non-NULL values [OK]
Hint: Use COUNT(DISTINCT column) for unique non-NULL counts [OK]
Common Mistakes:
  • Using COUNT(column) to count unique values
  • Using COUNT(*) with WHERE instead of DISTINCT
  • Using invalid syntax like COUNT(DISTINCT *)