Sub topic 11
Combining DISTINCT with ORDER BY and LIMIT
This chapter assumes you know basic SELECT and FROM syntax, can filter with WHERE, and can sort rows with ORDER BY. The goal is to return a limited list of unique values in a predictable order.
The basic pattern
DISTINCT removes duplicate result rows. ORDER BY sorts the remaining rows. LIMIT keeps only the first rows in that sorted result.
SELECT DISTINCT country
FROM customers
ORDER BY country
LIMIT 5;Suppose the customers table contains many customers from the same country. The query returns at most five different countries, sorted alphabetically. It does not return five customers.
The clauses have different jobs:
| Clause | Effect |
|---|---|
| `DISTINCT` | Removes duplicate result rows |
| `ORDER BY` | Sorts the result |
| `LIMIT` | Returns only the first rows |
The logical processing is important. Conceptually, the database reads the table, produces the selected values, removes duplicates, sorts the unique values, and then limits the result.
Sorting and limiting distinct combinations
With more than one selected column, DISTINCT applies to the complete combination of values.
SELECT DISTINCT country, city
FROM customers
ORDER BY country, city
LIMIT 10;Here, (Canada, Toronto) and (Canada, Vancouver) are different rows because their city values differ. The query sorts by country first and city second, then returns the first ten unique pairs.
When using SELECT DISTINCT, sort by a selected column when possible. Some database systems reject an ORDER BY expression that is not part of the distinct result. Even when a system accepts it, the intended ordering may be unclear.
A common mistake
This query returns five rows, but they may all have the same country:
SELECT country, city
FROM customers
ORDER BY country
LIMIT 5;It limits customer rows, not countries. To return unique countries, include DISTINCT:
SELECT DISTINCT country
FROM customers
ORDER BY country
LIMIT 5;Another common mistake is applying LIMIT before removing duplicates in your mental model. The intended result is usually “the first five unique values,” which requires the distinct operation to determine the candidate set before the limit is applied.
Combining filters
WHERE filters source rows before DISTINCT creates unique result rows.
SELECT DISTINCT country
FROM customers
WHERE active = TRUE
ORDER BY country
LIMIT 5;This returns the first five countries represented by active customers. An inactive customer from a country does not make that country appear unless at least one active customer has it.
For a stable result, make the ordering complete enough to distinguish rows. With one distinct country column, ORDER BY country is sufficient. With multiple columns, order by the same columns in a deliberate sequence:
SELECT DISTINCT country, city
FROM customers
ORDER BY country ASC, city ASC
LIMIT 10;Exercise
Exercise
Write a query that returns the three alphabetically first unique cities from active customers.
Show answer
SELECT DISTINCT city FROM customers WHERE active = TRUE ORDER BY city LIMIT 3;