Sub topic 3
DISTINCT Across Multiple Columns
Assume you already know basic SELECT and FROM queries, WHERE filters, and ORDER BY sorting. This chapter focuses on how DISTINCT behaves when a query selects more than one column.
The combination is the distinct value
Suppose a customers table contains these rows:
| customer_id | country | city |
|---|---|---|
| 1 | Canada | Toronto |
| 2 | Canada | Toronto |
| 3 | Canada | Vancouver |
| 4 | France | Paris |
A query that selects both country and city can repeat the same pair:
SELECT country, city
FROM customers;The two Toronto rows have different customer IDs, but the selected result contains the same values: Canada and Toronto. If the report needs each country-and-city pair only once, use DISTINCT:
SELECT DISTINCT country, city
FROM customers
ORDER BY country, city;The result is:
| country | city |
|---|---|
| Canada | Toronto |
| Canada | Vancouver |
| France | Paris |
DISTINCT removes repeated combinations of all selected columns. It does not remove duplicate values from each column independently.
Why column choice matters
Consider this query:
SELECT DISTINCT country, city, customer_id
FROM customers;It will usually return every customer. The customer ID is different for each row, so the complete combination is different. DISTINCT cannot recognize two rows as duplicates when any selected column differs.
Choose columns that represent the value you want to report:
- Use
DISTINCT countryfor unique countries. - Use
DISTINCT country, cityfor unique country-and-city pairs. - Include an identifier only when each identifier should remain separate.
A common mistake is selecting extra columns for convenience and then wondering why duplicates remain. If you need other details about one representative customer, DISTINCT alone is not enough. You may need grouping, a window function, or a database-specific rule for choosing that representative row.
DISTINCT with sorting and row limits
You can combine multiple-column DISTINCT with ORDER BY and LIMIT:
SELECT DISTINCT country, city
FROM customers
ORDER BY country, city
LIMIT 10;The database first forms unique country-and-city pairs, sorts those pairs, and returns the first ten. The sort is important because a query without ORDER BY does not promise a predictable order.
If you use OFFSET as well, the database skips rows in that sorted list:
SELECT DISTINCT country, city
FROM customers
ORDER BY country, city
LIMIT 10 OFFSET 20;This returns the third page when each page contains ten unique pairs.
Important edge cases
DISTINCT treats NULL values as one equivalent value for duplicate elimination. Therefore, several rows with NULL in the same selected positions produce one result for that combination.
For example, multiple rows with country Canada and city NULL produce one Canada, NULL pair. This does not mean NULL equals another value in ordinary comparisons. It means DISTINCT groups missing values together for its result.
When selecting several columns, the position of each value matters. These are different combinations:
| country | city |
|---|---|
| Canada | Toronto |
| Toronto | Canada |
They remain separate because the values occur in different columns.
Exercise
A query selects DISTINCT country, city, customer_id, but duplicate country-and-city pairs still appear. What causes this, and how should the query be changed if only the pairs are needed?
Show answer
customer_id makes each complete selected row different. Select only country and city: SELECT DISTINCT country, city FROM customers;