Stop 05 · Aggregation & GroupingTopics · 2/9

COUNT(*) vs COUNT(column) vs COUNT(DISTINCT)

Part of the SQL Roadmap

In short

"How many?" sounds like the easiest question in SQL, yet three different COUNTs give three different answers. Picking the wrong one is how a report says 6 buyers when there were really 2.

COUNT(*) counts all rows, COUNT(column) counts rows where that column is not NULL, and COUNT(DISTINCT column) counts unique non-NULL values.

The jaadu stands at a marigold-decorated wedding gate with a clicker in one hand, tallying a line of guests while some families walk in together holding hands.

Think of it like this

The wedding guest register

At a wedding gate, the guard counts every person who walks in (COUNT(*)). The register only has entries for guests who wrote their phone number (COUNT(column)). And the family elders want to know how many different families came (COUNT(DISTINCT)). Same crowd, three honest answers.

Watch it happen

The coupon_code column of six October orders

DIWALI10
0
NULL
1
FEST20
2
DIWALI10
3
NULL
4
DIWALI10
5

1/5Six orders. Two customers didn't use a coupon, so coupon_code is NULL for them.

The big ideas

What you need to know

COUNT(*) counts rows

It never looks inside the row, so NULLs don't matter. Use it for 'how many orders'.

COUNT(column) skips NULL

Only rows where that column has a value are counted. Great for 'how many orders had a coupon'.

COUNT(DISTINCT) counts unique

Repeats are counted once and NULL is ignored. Use it for buyers, cities or products, not orders.

Compare them on purpose

Running all three side by side is a quick data check. A gap between them always has a story.

See it in code

Walk through the code

sql
1SELECT2  COUNT(*)                    AS total_orders,3  COUNT(coupon_code)          AS orders_with_coupon,4  COUNT(DISTINCT coupon_code) AS different_coupons,5  COUNT(DISTINCT customer_id) AS unique_buyers6FROM orders7WHERE order_date >= DATE '2026-10-01'8  AND order_date <  DATE '2026-11-01';

1/4Every October order, no matter what is NULL.

Four counts for October in one row. This works the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

Reporting COUNT(*) as 'number of customers'.

Do this instead

Use COUNT(DISTINCT customer_id). Priya with five orders is still one customer.

Oops

Using COUNT(phone) to count all customers.

Do this instead

COUNT(column) silently drops rows where phone is NULL. Use COUNT(*) or COUNT(customer_id) on a column that is never NULL.

Oops

Expecting COUNT(DISTINCT city) to include a 'blank' city group.

Do this instead

NULL is never counted by COUNT(DISTINCT). Count NULLs separately with COUNT(*) - COUNT(city) if they matter.

Your mission

Three counts, one story

~15 min

Quick check

A city column has values Pune, Pune, NULL, Delhi, NULL. What does COUNT(DISTINCT city) return?

Go deeper

  1. 1Run all three on the same column and explain each number.
  2. 2Use COUNT(DISTINCT customer_id) to count buyers, not orders.
  3. 3Remember this difference, it is a very common interview question.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge