Sub topic 5
How DISTINCT Treats `NULL` Values
Assume you know SELECT, FROM, WHERE, and ORDER BY. This chapter focuses on how SELECT DISTINCT and COUNT(DISTINCT ...) handle NULL, which represents a missing or unknown value.
`NULL` is not an ordinary value
NULL means that a value is absent, unknown, or not applicable. It is not the same as zero, an empty string, or the text "NULL".
Comparisons with NULL use three-valued logic. A comparison such as country = NULL does not return true. Use IS NULL or IS NOT NULL when testing for it.
SELECT customer_id, country
FROM customers
WHERE country IS NULL;NULL also does not equal another NULL in an ordinary comparison:
SELECT NULL = NULL AS comparison_result;The result is unknown, not true. However, DISTINCT uses duplicate-elimination rules that group all NULL values together.
`SELECT DISTINCT` returns one `NULL`
Suppose a table contains these country values:
| country |
|---|
| Canada |
| `NULL` |
| Canada |
| `NULL` |
| Japan |
This query returns each different country once:
SELECT DISTINCT country
FROM customers
ORDER BY country;The result contains three rows: Canada, Japan, and one NULL row. All missing country values count as one distinct result for this operation.
The result is different from filtering with equality. DISTINCT does not ask whether one NULL equals another. It removes repeated representations of the same selected value, and treats the NULL group as a single output value.
You can make the missing group easier to read with COALESCE, which replaces NULL with another value:
SELECT DISTINCT COALESCE(country, 'Unknown') AS country_label
FROM customers
ORDER BY country_label;Use a replacement label only when it is appropriate for the report. It changes the displayed result, and the label could collide with a real stored value.
`COUNT(DISTINCT ...)` ignores `NULL`
COUNT(DISTINCT country) counts unique non-NULL country values. It does not count the NULL group.
SELECT COUNT(DISTINCT country) AS country_count
FROM customers;For the sample values above, country_count is 2, because only Canada and Japan are counted.
This differs from SELECT DISTINCT, which displays one NULL row. Compare these operations:
| Expression | What it returns |
|---|---|
| `SELECT DISTINCT country` | Unique countries plus one `NULL` row, if present |
| `COUNT(DISTINCT country)` | Number of unique non-`NULL` countries |
| `COUNT(country)` | Number of non-`NULL` country rows |
| `COUNT(*)` | Number of rows, including rows with `NULL` country |
To count missing values separately, use a conditional expression:
SELECT
COUNT(DISTINCT country) AS known_country_count,
COUNT(*) - COUNT(country) AS missing_country_count
FROM customers;COUNT(country) ignores NULL, while COUNT(*) counts every row. Their difference is therefore the number of rows whose country is missing.
`NULL` in multiple selected columns
With more than one selected column, DISTINCT removes repeated combinations. Two rows are duplicates for this purpose when every selected column has the same value, including both columns being NULL.
SELECT DISTINCT country, city
FROM customers
ORDER BY country, city;A row with (NULL, 'Toronto') is different from (NULL, 'Vancouver'). A second (NULL, 'Toronto') is removed as a duplicate.
Exercise
A table has 12 rows with country = NULL, plus 4 different non-NULL countries. What do SELECT DISTINCT country and COUNT(DISTINCT country) report?
Show answer
SELECT DISTINCT country reports 5 rows: the 4 countries and one NULL row. COUNT(DISTINCT country) reports 4 because it ignores NULL.