COUNT(*) vs COUNT(column) difference in SQL - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how counting rows in a table changes as the table grows.
Specifically, we compare counting all rows versus counting only rows with a value in a column.
Analyze the time complexity of these two queries.
SELECT COUNT(*) FROM employees;
SELECT COUNT(salary) FROM employees;
The first counts all rows, the second counts only rows where salary is not NULL.
Both queries scan the table rows once.
- Primary operation: Checking each row in the employees table.
- How many times: Once per row, for all rows in the table.
As the number of rows grows, the work grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the counting work grows in direct proportion to the number of rows.
[X] Wrong: "COUNT(*) is slower than COUNT(column) because it counts everything."
[OK] Correct: Both scan all rows; COUNT(column) just skips NULLs but still checks each row, so their time grows the same way.
Understanding how simple counting queries scale helps you explain database performance clearly and confidently.
"What if the column used in COUNT(column) has an index? How would that affect the time complexity?"
Practice
COUNT(*) and COUNT(column_name) in SQL?Solution
Step 1: Understand
COUNT(*)COUNT(*)counts every row in the table, including those with NULL values in any column.Step 2: Understand
COUNT(column_name)COUNT(column_name)counts only rows where the specified column is NOT NULL, ignoring rows where that column is NULL.Final Answer:
COUNT(*)counts all rows;COUNT(column_name)counts only non-NULL values in that column. -> Option DQuick Check:
COUNT(*) counts all rows, COUNT(column) skips NULLs [OK]
- Thinking COUNT(column) counts NULL values
- Assuming COUNT(*) ignores NULLs
- Believing both always return same count
email is NOT NULL?Solution
Step 1: Analyze
COUNT(email)COUNT(email)counts only rows whereemailis NOT NULL, so it already filters NULLs.Step 2: Check the WHERE clause necessity
AddingWHERE email IS NOT NULLis redundant withCOUNT(email), so SELECT COUNT(email) FROM users; is correct and simpler.Final Answer:
SELECT COUNT(email) FROM users; -> Option AQuick Check:
COUNT(column) counts non-NULL rows without WHERE [OK]
- Adding unnecessary WHERE clause with COUNT(column)
- Using COUNT(*) with wrong WHERE condition
- Confusing NULL and NOT NULL filters
orders with 5 rows where the discount column has values: 10, NULL, 5, NULL, 0, what will be the result of SELECT COUNT(*) AS total, COUNT(discount) AS discount_count FROM orders;?Solution
Step 1: Count total rows with
COUNT(*)COUNT(*)counts all 5 rows regardless of NULLs.Step 2: Count non-NULL
Only 3 rows have non-NULL discount values (10, 5, 0), so discount_count is 3.discountvalues withCOUNT(discount)Final Answer:
total = 5, discount_count = 3 -> Option AQuick Check:
COUNT(*) = all rows, COUNT(column) = non-NULL rows [OK]
- Counting NULLs in COUNT(column)
- Confusing total rows with non-NULL counts
- Assuming 0 is NULL
SELECT COUNT(column_name) FROM table_name; but it returns 0. The column has some NULL and some non-NULL values. What is the most likely problem?Solution
Step 1: Check column existence
IfCOUNT(column_name)returns 0 but column has non-NULL values, likely the column name is wrong or missing.Step 2: Understand COUNT behavior
COUNT(column_name)counts non-NULL values; if column exists and has non-NULLs, result won't be zero.Final Answer:
Column name is misspelled or does not exist. -> Option BQuick Check:
Wrong column name causes zero count [OK]
- Assuming COUNT(column) counts NULLs
- Using COUNT(*) when column is misspelled
- Ignoring empty table possibility without checking
employees with 100 rows. The phone_number column has 80 non-NULL values and 20 NULLs. You want to find how many employees have a phone number and how many total employees there are. Which query gives both counts correctly?Solution
Step 1: Count employees with phone numbers
COUNT(phone_number)counts only non-NULL phone numbers, so it returns 80.Step 2: Count total employees
COUNT(*)counts all rows, so it returns 100.Final Answer:
SELECT COUNT(phone_number) AS with_phone, COUNT(*) AS total_employees FROM employees; -> Option CQuick Check:
COUNT(phone_number) = 80, COUNT(*) = 100 [OK]
- Swapping counts in SELECT clause
- Adding counts instead of separate columns
- Confusing which count counts NULLs
