Sub topic 4
Counting Unique Values with `COUNT(DISTINCT)`
This chapter assumes that you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also know that a table contains rows and columns.
A query can return repeated values. For example, many customers may live in the same country. If you want to count the countries represented in the table, counting rows gives the wrong result. COUNT(DISTINCT column) counts different non-NULL values in a column.
Counting different values
Suppose customers contains these rows:
| customer_id | country |
|---|---|
| 1 | Canada |
| 2 | Canada |
| 3 | Japan |
| 4 | `NULL` |
There are four rows, but only two non-NULL country values: Canada and Japan.
SELECT COUNT(DISTINCT country) AS country_count
FROM customers;The result is:
| country_count |
|---|
| 2 |
DISTINCT removes repeated values before COUNT counts them. The AS country_count part gives the result column a readable name.
This differs from the following expressions:
| Expression | What it counts |
|---|---|
| `COUNT(*)` | Every row, including rows with `NULL` values |
| `COUNT(country)` | Rows where `country` is not `NULL` |
| `COUNT(DISTINCT country)` | Different non-`NULL` country values |
What happens before counting
You can filter rows before counting unique values. The filter changes the input set, and COUNT(DISTINCT) counts unique values in that filtered set.
SELECT COUNT(DISTINCT country) AS country_count
FROM customers
WHERE signup_date >= '2024-01-01';This counts countries among customers who signed up on or after January 1, 2024. It does not count countries across the entire table.
You can also count unique values for each group. For example, suppose orders has a sales_region and a customer_id. This query counts different customers who placed orders in each region:
SELECT
sales_region,
COUNT(DISTINCT customer_id) AS customer_count
FROM orders
GROUP BY sales_region
ORDER BY sales_region;A customer who placed several orders in one region counts once in that region. If the same customer appears in two regions, that customer can count once in each region because each group is counted separately.
Multiple columns
The exact syntax for counting distinct combinations differs between database systems. COUNT(DISTINCT country) counts values from one column. It does not count unique pairs such as (country, city).
To count unique pairs in a widely supported way, first select distinct pairs in a subquery, then count the resulting rows:
SELECT COUNT(*) AS location_count
FROM (
SELECT DISTINCT country, city
FROM customers
) AS locations;The subquery produces one row for each unique country-and-city pair. The outer query counts those rows. Pairs containing NULL may be returned by the subquery, but database behavior and requirements should be checked when NULL has special meaning in the data.
Common errors
- Using
COUNT(*)when the question asks for different values. - Forgetting that
NULLis excluded fromCOUNT(DISTINCT column). - Counting unique values before applying the required
WHEREfilter. - Assuming that
COUNT(DISTINCT country, city)works identically in every database system. - Giving the result an unclear name, which makes reports and application code harder to read.
Exercise
A customers table has 1,200 rows. The country column contains 18 different non-NULL values, and 40 rows contain NULL. What do COUNT(*), COUNT(country), and COUNT(DISTINCT country) return?
Show answer
COUNT(*) returns 1,200. COUNT(country) returns 1,160 because it excludes the 40 NULL values. COUNT(DISTINCT country) returns 18 because it counts each different non-NULL country once.