Bird
Raised Fist0
SQLquery~10 mins

COUNT function behavior in SQL - Step-by-Step Execution

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
Concept Flow - COUNT function behavior
Start Query
Scan Table Rows
Check Each Row for COUNT Criteria
If COUNT(*)
If COUNT(column)
Aggregate Count
Return Result
The COUNT function scans table rows, counts all rows or only those with non-null values depending on usage, then returns the total count.
Execution Sample
SQL
SELECT COUNT(*) FROM employees;
SELECT COUNT(salary) FROM employees;
Counts total rows and counts rows with non-null salary values in the employees table.
Execution Table
StepRow Data (id, salary)COUNT(*) Running TotalCOUNT(salary) Running TotalAction
1(1, 5000)11Count row for both COUNT(*) and COUNT(salary) because salary is not NULL
2(2, NULL)21Count row for COUNT(*) only; salary is NULL so not counted in COUNT(salary)
3(3, 7000)32Count row for both COUNT(*) and COUNT(salary)
4(4, 0)43Count row for both COUNT(*) and COUNT(salary); zero is counted as value
5(5, NULL)53Count row for COUNT(*) only; salary NULL not counted in COUNT(salary)
EndNo more rows53Finished counting all rows
💡 All rows processed; COUNT(*) counts all rows, COUNT(salary) counts only rows with non-null salary
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
COUNT(*)0123455
COUNT(salary)0112333
Key Moments - 2 Insights
Why does COUNT(*) count more rows than COUNT(column) in the example?
COUNT(*) counts every row regardless of NULLs, while COUNT(column) only counts rows where the column value is NOT NULL, as shown in execution_table rows 2 and 5.
Does COUNT(column) count rows where the column value is zero?
Yes, zero is a valid value and not NULL, so COUNT(column) includes it, as seen in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is COUNT(salary) after processing row 3?
A1
B2
C3
D0
💡 Hint
Check the COUNT(salary) Running Total column at Step 3 in execution_table.
At which step does COUNT(*) reach 4?
AAfter processing row 2
BAfter processing row 3
CAfter processing row 4
DAfter processing row 5
💡 Hint
Look at the COUNT(*) Running Total column in execution_table for each step.
If all salary values were NOT NULL, how would COUNT(salary) compare to COUNT(*) at the end?
ACOUNT(salary) would be equal to COUNT(*)
BCOUNT(salary) would be zero
CCOUNT(salary) would be less than COUNT(*)
DCOUNT(salary) would be greater than COUNT(*)
💡 Hint
Refer to the rule that COUNT(column) counts only non-null values; if none are NULL, counts match.
Concept Snapshot
COUNT(*) counts all rows including those with NULLs.
COUNT(column) counts only rows where column is NOT NULL.
NULL values are ignored by COUNT(column).
Zero and empty strings are counted by COUNT(column).
Use COUNT(*) to get total rows regardless of NULLs.
Full Transcript
The COUNT function in SQL counts rows in a table. COUNT(*) counts every row no matter what. COUNT(column) counts only rows where the column has a value that is not NULL. For example, if a table has 5 rows but some rows have NULL in the salary column, COUNT(*) returns 5 but COUNT(salary) returns fewer. Zero values count as valid values for COUNT(column). This visual trace shows step-by-step how the counts increase as each row is checked. It helps understand why COUNT(*) and COUNT(column) can return different results.

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 *)