1. Home
  2. Interview Questions
  3. SQL
  4. 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

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best 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 supports COUNT(*) 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

All easy questions →

Related Questions

‹
Q6 of 10 · SQL
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA