COUNT function behavior in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to count rows grows as the table gets bigger.
How does the COUNT function behave when counting many rows?
Analyze the time complexity of the following code snippet.
SELECT COUNT(*) FROM orders;
This query counts all rows in the orders table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the orders table once.
- How many times: Once for every row in the table.
As the number of rows grows, the counting work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 row checks |
| 100 | 100 row checks |
| 1000 | 1000 row checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means counting rows takes longer as the table gets bigger, growing in a straight line with the number of rows.
[X] Wrong: "COUNT(*) is instant no matter how big the table is."
[OK] Correct: The database must look at each row to count it, so more rows mean more work.
Knowing how COUNT scales helps you understand query speed and database behavior in real projects.
"What if we count only rows where a column has a specific value? How would the time complexity change?"
Practice
COUNT(*) do when used in a query?Solution
Step 1: Understand COUNT(*) behavior
TheCOUNT(*)function counts every row in the table regardless of NULL values in any column.Step 2: Compare with other COUNT variants
UnlikeCOUNT(column), which skips NULLs,COUNT(*)includes all rows.Final Answer:
Counts all rows in the table, including those with NULL values -> Option CQuick Check:
COUNT(*) counts all rows [OK]
- Thinking COUNT(*) skips NULL rows
- Confusing COUNT(*) with COUNT(column)
- Assuming COUNT(*) counts distinct values
age from the table persons?Solution
Step 1: Identify how to count non-NULL values
COUNT(column)counts only non-NULL values in that column.Step 2: Check syntax correctness
SELECT COUNT(age) FROM persons;correctly counts non-NULLagevalues.Final Answer:
SELECT COUNT(age) FROM persons; -> Option BQuick Check:
COUNT(column) counts non-NULL values [OK]
- Using COUNT(*) to count non-NULL values
- Using WHERE clause unnecessarily
- Using COUNT with boolean expressions
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;?Solution
Step 1: Identify distinct non-NULL values in department
The distinct non-NULL values are 'HR', 'IT', and 'Finance'. That's 3 unique values.Step 2: Understand COUNT(DISTINCT) behavior
COUNT(DISTINCT column) counts unique non-NULL values only, so NULLs are excluded.Step 3: Count distinct values
There are 3 distinct non-NULL values, so the count is 3.Final Answer:
3 -> Option AQuick Check:
COUNT(DISTINCT) excludes NULLs [OK]
- Including NULL as a distinct value
- Counting total rows instead of distinct
- Confusing COUNT(*) with COUNT(DISTINCT)
SELECT COUNT(employee_id) FROM staff; but it returns 0 even though the table has rows. What is the most likely reason?Solution
Step 1: Understand COUNT(column) behavior
COUNT(column) counts only non-NULL values in that column.Step 2: Analyze why count is zero
If allemployee_idvalues are NULL, COUNT returns 0 even if rows exist.Final Answer:
The column employee_id contains only NULL values -> Option AQuick Check:
COUNT(column) excludes NULLs [OK]
- Assuming COUNT(column) counts all rows
- Thinking table is empty without checking data
- Assuming syntax error without checking query
Solution
Step 1: Understand requirement for unique customers excluding NULLs
We need to count distinct customer IDs ignoring NULLs.Step 2: Analyze options
SELECT COUNT(DISTINCT customer_id) FROM orders; usesCOUNT(DISTINCT customer_id), which counts unique non-NULL values correctly.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.Final Answer:
SELECT COUNT(DISTINCT customer_id) FROM orders; -> Option DQuick Check:
COUNT(DISTINCT column) counts unique non-NULL values [OK]
- Using COUNT(column) to count unique values
- Using COUNT(*) with WHERE instead of DISTINCT
- Using invalid syntax like COUNT(DISTINCT *)
