- Home
- Interview Questions
- SQL
- What is the difference between COUNT(*), COUNT(column) and C…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: COUNT(*) counts every row in the group, including rows that contain NULLs. COUNT(column) counts only rows where that column is not NULL, and COUNT(DISTINCT column) counts the unique non-NULL values in that column.
Detailed explanation
All aggregate functions except COUNT(*) ignore NULLs. That one rule explains most of the differences between the three forms.
- COUNT(*) counts rows. It does not look at any column value, so NULLs make no difference.
COUNT(1)gives the same result. Modern optimisers treat the two identically, so neither is faster. - COUNT(column) counts rows where that expression is not NULL. If 3 out of 10 employees have no manager,
COUNT(manager_id)returns 7. - COUNT(DISTINCT column) removes duplicate values and ignores NULLs, then counts what is left. It answers questions like "how many different departments have employees?"
Two related behaviours often come up in interviews:
- Empty input: COUNT returns 0 when no rows match. SUM, AVG, MIN and MAX return NULL instead. That is why reports often wrap SUM in
COALESCE(SUM(x), 0). - Conditional counting: because COUNT skips NULLs,
COUNT(CASE WHEN status = 'PAID' THEN 1 END)counts only paid rows. When the condition is false, the CASE returns NULL, and that NULL is not counted. PostgreSQL also supportsCOUNT(*) FILTER (WHERE status = 'PAID').
The biggest practical trap involves LEFT JOIN. When a parent row has no matching child rows, the join still produces one row for it, with NULLs in the child columns. COUNT(*) counts that row and returns 1. COUNT(child.id) correctly returns 0.
Example
-- employees(emp_id, name, dept_id, manager_id)
-- 5 rows; two rows have manager_id NULL; dept_id values: 10,10,20,20,NULL
SELECT COUNT(*) AS total_rows, -- 5
COUNT(manager_id) AS has_manager, -- 3
COUNT(dept_id) AS has_dept, -- 4
COUNT(DISTINCT dept_id) AS distinct_depts -- 2
FROM employees;
-- LEFT JOIN trap: customers with zero orders
SELECT c.customer_id,
COUNT(*) AS wrong_count, -- 1 for a customer with no orders
COUNT(o.order_id) AS right_count -- 0 for a customer with no orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id;
-- Conditional counts in one pass
SELECT COUNT(CASE WHEN status = 'PAID' THEN 1 END) AS paid,
COUNT(CASE WHEN status = 'PENDING' THEN 1 END) AS pending
FROM orders;Common mistakes
- Using COUNT(*) after a LEFT JOIN and reporting 1 order for customers who have never ordered.
- Assuming COUNT(DISTINCT col) counts NULL as one of the distinct values. It does not. Add 1 manually if you need NULL treated as a group, or use GROUP BY instead.
- Writing
COUNT(CASE WHEN status = 'PAID' THEN 1 ELSE 0 END). The ELSE 0 is not NULL, so every row is counted. Either drop the ELSE or switch to SUM. - Claiming COUNT(1) is faster than COUNT(*). In mainstream databases they produce the same plan.
Follow-up questions
- Why does SUM return NULL on an empty table while COUNT returns 0?
- How would you count distinct combinations of two columns in a database that does not support COUNT(DISTINCT a, b)?
- Is COUNT(*) on a very large table expensive, and how might you get an approximate count instead?
Interview tip: Quote the rule "every aggregate except COUNT(*) ignores NULLs" first, then use the LEFT JOIN example to show you have hit this in real work.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More easy SQL questions
Related Questions
- What is the difference between a view and a materialized view, and when would you use each? · SQL · 1 ans
- How do you fetch the entire row with the highest salary in each department? · SQL · 1 ans
- How does a recursive CTE execute step by step, and how do you stop it looping forever? · SQL · 1 ans
- Why can wrapping an indexed column in a function in WHERE make a query slow? · SQL · 1 ans
- Why does WHERE column = NULL return no rows, and how should you filter NULLs correctly? · SQL · 1 ans
- What are insertion, update and deletion anomalies, and how does normalization prevent them? · SQL · 1 ans
- How does BCNF differ from 3NF, and why can BCNF decomposition lose a dependency? · SQL · 1 ans
- How do ON DELETE CASCADE, SET NULL and RESTRICT work, and when are cascades dangerous? · SQL · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)