SQL reference

SQL COUNT DISTINCT: count unique values

In Excel, you'd reach for
fx=COUNTA(UNIQUE(range)), or a pivot with "Distinct Count"

"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
montheventsactive_customers
2026-0120148
2026-0245589
2026-03787144
2026-04926188
2026-051221248
2026-061324280

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

How many different products has each region sold? Columns region and products.

This page on one card

SQL COUNT DISTINCT: count unique values: the Excel way (=COUNTA(UNIQUE(range))) and the SQL query, side by side
SQL COUNT DISTINCT: count unique values, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows