SQL COUNT DISTINCT: count unique values
"How many orders" and "how many customers ordered" are different questions. The second needs each customer counted once, however many times they appear. That's COUNT(DISTINCT customer_id).
It's the engine of every "active users" number: distinct people who did something in a period.
Syntax
SELECT COUNT(DISTINCT column)
FROM table;
Try itEdit the query, then Run
| month | events | active_customers |
|---|---|---|
| 2026-01 | 201 | 48 |
| 2026-02 | 455 | 89 |
| 2026-03 | 787 | 144 |
| 2026-04 | 926 | 188 |
| 2026-05 | 1221 | 248 |
| 2026-06 | 1324 | 280 |
6 rows
To count distinct combinations of columns, most databases need a subquery: SELECT COUNT(*) FROM (SELECT DISTINCT region, product_id FROM orders). See the weekly active users recipe for this in action.
Your turn
Solved
How many different products has each region sold? Columns region and products.
COUNT(DISTINCT product_id) with GROUP BY region.
This page on one card
