PostgreSQL SELECT DISTINCT
A column often contains the same value many times: fifteen customers come from only thirteen countries, and twenty orders have just four different statuses. SELECT DISTINCT removes duplicate rows from the result so that each combination appears once.
Removing duplicates
Compare the plain query with the DISTINCT one:
SELECT country FROM customers ORDER BY country;
SELECT DISTINCT country FROM customers ORDER BY country; country
-------------
Canada
France
Germany
Ireland
Italy
Japan
Lebanon
Poland
Portugal
South Korea
Sweden
UK
UK
UK
USA
(15 rows)
country
-------------
Canada
France
Germany
Ireland
Italy
Japan
Lebanon
Poland
Portugal
South Korea
Sweden
UK
USA
(13 rows)
The UK appears three times in the first result and once in the second.
Distinct combinations
With several columns, DISTINCT looks at the whole row. Two rows are duplicates only if every selected column matches:
SELECT DISTINCT customer_id, status
FROM orders
WHERE customer_id IN (1, 2, 10)
ORDER BY customer_id, status; customer_id | status
-------------+-----------
1 | delivered
2 | delivered
2 | pending
10 | cancelled
10 | delivered
(5 rows)
Customer 2 has three orders: two delivered and one pending, so it appears in two rows. Customer 10 has one delivered and one cancelled order.
Counting distinct values
count(DISTINCT column) counts how many different values a column has, ignoring NULLs:
SELECT count(*) AS orders,
count(DISTINCT customer_id) AS customers_who_ordered,
count(DISTINCT status) AS statuses
FROM orders; orders | customers_who_ordered | statuses
--------+-----------------------+----------
20 | 12 | 4
(1 row)
DISTINCT ON: the first row of each group
DISTINCT ON (expression) is a PostgreSQL extension that keeps only the first row for each distinct value of the expression. "First" is decided by ORDER BY, which must start with the same expression. It is the simplest way to answer questions such as "what is the most expensive book in each category?":
SELECT DISTINCT ON (category_id)
category_id, title, price
FROM books
ORDER BY category_id, price DESC; category_id | title | price
-------------+----------------------+-------
1 | Winter in Sofia | 17.80
2 | Clockwork Garden | 21.00
3 | The Quiet Witness | 13.49
4 | Postgres in Practice | 44.00
5 | Empires of Sand | 27.00
6 | Ten Tiny Dragons | 8.99
(6 rows)
Read it as: sort by category, and within each category by price from high to low, then keep the first row of every category. Another example: each customer's most recent order.
SELECT DISTINCT ON (customer_id)
customer_id, id AS order_id, order_date, status
FROM orders
ORDER BY customer_id, order_date DESC
LIMIT 5; customer_id | order_id | order_date | status
-------------+----------+------------+-----------
1 | 12 | 2024-07-09 | delivered
2 | 18 | 2024-11-11 | pending
3 | 4 | 2024-02-14 | delivered
4 | 15 | 2024-09-14 | delivered
5 | 16 | 2024-10-05 | shipped
(5 rows)
Note: Without the extra sort column, which row "wins" in
DISTINCT ONis unpredictable. Always add anORDER BYthat makes the choice explicit.
NULLs and DISTINCT
For DISTINCT, NULLs are treated as equal to each other, so all NULLs collapse into a single row:
SELECT DISTINCT birth_year IS NULL AS unknown_birth_year
FROM authors
ORDER BY 1; unknown_birth_year
--------------------
f
t
(2 rows)
Test yourself
With SELECT DISTINCT city, country, when are two rows considered duplicates?
- When the city matches
- When the country matches
- When both city and country match
- Never
What does count(DISTINCT customer_id) count?
- All orders
- Different customer ids, ignoring NULL
- Customers without orders
- Duplicate ids
In SELECT DISTINCT ON (category_id) ... ORDER BY category_id, price DESC, which book is kept per category?
- The cheapest
- The most expensive
- A random one
- The newest