COUNT(*) vs COUNT(column) vs COUNT(DISTINCT column)
Know which COUNT variant ignores NULLs and when DISTINCT changes the answer.
In plain words
COUNT(*) counts every row, COUNT(column) skips blanks, and COUNT(DISTINCT column) counts each different value once.
Think of it like…
A class register: COUNT(*) is how many names are on the list, COUNT(phone) is how many students gave a phone number, and COUNT(DISTINCT city) is how many different cities they come from.
Where you’ve seen it
A signups table with 1,000 rows, 800 emails filled in, and 760 unique emails: COUNT(*) = 1,000, COUNT(email) = 800, COUNT(DISTINCT email) = 760. Each answers a different business question.
Words you’ll hear
- COUNT(*)
- Number of rows, including rows full of NULLs.
- COUNT(column)
- Number of rows where that column is not NULL.
- COUNT(DISTINCT column)
- Number of different non-NULL values.
- COUNT(1)
- Exactly the same as COUNT(*); not faster.
Visual diagram
Theory & example queries
What each variant counts
COUNT(*) counts rows, regardless of their contents. COUNT(expr) evaluates the expression for each row and counts the non-NULL results. COUNT(DISTINCT expr) removes duplicates first, then counts the remaining non-NULL values.
COUNT(1), COUNT('x') and COUNT(*) all count rows, because the constant is never NULL. Modern optimizers treat them identically, so the idea that COUNT(1) is faster is a myth. Unlike SUM, AVG and MAX, COUNT over zero rows returns 0, not NULL.
SELECT COUNT(*) AS signups, COUNT(email) AS with_email, COUNT(DISTINCT email) AS unique_emailsFROM signups;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output1 sample row
| signups | with_email | unique_emails |
|---|---|---|
| 1000 | 940 | 917 |
Note: Illustrative counts: 60 NULL emails and 23 repeated non-NULL email values are excluded by the respective COUNT rules.
Key takeaway: Rows, non-NULL values, distinct non-NULL values.
Counting after joins, and the cost of DISTINCT
After customers LEFT JOIN orders, a customer with no orders still produces one row, so COUNT(*) gives them 1 order. COUNT(o.id) gives 0 because o.id is NULL on that row. Always count the key of the table whose rows you mean.
COUNT(DISTINCT) has to remember every value it has seen, so on large data it's much more expensive than COUNT(*). For dashboards over billions of rows, analytical engines offer approximate versions such as APPROX_COUNT_DISTINCT (BigQuery, Snowflake) that use HyperLogLog. In PostgreSQL, an exact COUNT(*) on a huge table needs a full scan; pg_class.reltuples gives a fast estimate.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT c.id, COUNT(o.id) AS ordersFROM customers c LEFT JOIN orders o ON o.customer_id = c.idGROUP BY c.id;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output4 sample rows
| id | orders |
|---|---|
| 1 | 3 |
| 2 | 0 |
| 3 | 7 |
| 4 | 1 |
Note: Illustrative customers; COUNT(o.id) remains 0 for customer 2 because the unmatched order side contains NULL.
Key takeaway: Count the key of the table you mean; DISTINCT costs memory.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Say what you're counting
Rows (signups), filled-in values (signups with an email) or different values (unique emails)? The wording decides the function.
rows · non-NULL · distinctWhen to use it & trade-offs
What it is
**COUNT(*) counts rows. COUNT(column) counts rows where that column is not NULL. COUNT(DISTINCT column)** counts the different non-NULL values. On the same data they can give three different answers.
Why it matters: Picking the wrong one quietly changes a metric: "number of users" vs "number of users with an email" vs "number of distinct emails". It also matters after joins, where COUNT(*) counts NULL-padded rows that COUNT(column) ignores.
When to reach for it
- COUNT(*) for volume metrics
- COUNT(column) for data-completeness checks and counts after LEFT JOIN
- COUNT(DISTINCT user_id) for unique users or active users
When to avoid or be careful
- COUNT(*) after a LEFT JOIN when you mean "related rows"
- COUNT(DISTINCT) over billions of rows when an approximate count would do
- Assuming COUNT(1) is faster than COUNT(*)
COUNT(*) vs COUNT(column) vs COUNT(DISTINCT column)
Strengths: Fastest and unambiguous.
Watch out: Counts NULL-padded rows after outer joins.
Strengths: Ignores NULLs automatically, which makes it correct after LEFT JOIN when you count the right table's key.
Watch out: Easy to confuse with a row count when the column is nullable.
Strengths: Counts each value once.
Watch out: Expensive on large data; still ignores NULLs.
Interview signals & numbers to know
When this topic comes up
- “How many unique users?”
- “COUNT(*) vs COUNT(column)”
- “Why is the count too high after the join?”
- Data completeness
- Daily or monthly active users
Benchmarks & rules of thumb
- 0
- COUNT over an empty set
- NULL
- SUM over an empty set
- COUNT(1)
- identical to COUNT(*)
Common mistakes & follow-ups
Common interview mistakes
- Using COUNT(*) after a LEFT JOIN and reporting 1 order for customers who have none.
- Counting COUNT(email) and calling it the number of users.
- Expecting COUNT(DISTINCT col) to count NULL as a value.
Likely interviewer follow-ups
- What does COUNT(DISTINCT col) return when every value is NULL?
- How would you count distinct combinations of two columns?
- How would you count rows matching a condition without a WHERE clause, in the same query as other counts?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply COUNT(*) vs COUNT(column) vs COUNT(DISTINCT column) with hands-on SQL practice and interview Q&A.