Sub topic 2
Removing Duplicates with `SELECT DISTINCT`
Assume you know basic SELECT and FROM syntax, can filter rows with WHERE, and can sort results with ORDER BY. This chapter explains how DISTINCT changes the rows returned by a query.
Why duplicate rows appear
A query result can contain repeated values even when the table has no duplicate rows. For example, a customers table might contain several customers from the same country:
| customer_id | name | country |
|---|---|---|
| 1 | Ana | Canada |
| 2 | Ben | Canada |
| 3 | Chen | Japan |
This query returns three rows:
SELECT country
FROM customers;The result contains Canada twice because the query returns one result row for each matching table row. The database does not automatically remove repeated values.
Duplicates can also appear after a JOIN. If one customer has many orders, joining customers to orders can repeat that customer's name once per order. Whether those repeated rows are correct depends on what the query is meant to show.
Removing duplicates with `SELECT DISTINCT`
DISTINCT is a keyword that keeps only unique result rows:
SELECT DISTINCT country
FROM customers;The result contains one row for each different country:
| country |
|---|
| Canada |
| Japan |
DISTINCT applies to the complete set of expressions in the SELECT list. It does not remove duplicate rows from the table itself. It only changes the result returned by this query.
You can combine it with filtering:
SELECT DISTINCT country
FROM customers
WHERE country IS NOT NULL
ORDER BY country;The WHERE clause first excludes rows with no country. The query then returns each remaining country once and sorts the result.
DISTINCT across multiple columns
When you select more than one column, uniqueness applies to the combination of values:
SELECT DISTINCT country, city
FROM customers
ORDER BY country, city;Two customers in the same country and city produce one result row. Customers in the same country but different cities produce separate rows.
| country | city |
|---|---|
| Canada | Montreal |
| Canada | Toronto |
| Japan | Osaka |
Think of each selected row as a single composite value. DISTINCT removes another row only when every selected column has the same value.
`DISTINCT` and `NULL`
NULL means an unknown or missing value. For duplicate elimination, multiple NULL values in one selected column are treated as one distinct result value:
SELECT DISTINCT country
FROM customers;If ten customers have country set to NULL, the result contains one NULL row, along with the non-NULL countries. This behavior is useful when you want to know which values, including “missing,” occur in the data.
DISTINCT does not replace data validation. If a country should always be present, investigate why NULL values exist rather than simply removing them.
Choosing the right level of uniqueness
Before adding DISTINCT, identify what should be unique:
- Use
DISTINCT countryfor unique countries. - Use
DISTINCT country, cityfor unique country-and-city pairs. - Include an identifier such as
customer_idwhen each entity must remain separate.
Selecting too few columns can hide important differences. For example, DISTINCT country merges all customers in one country because customer identity is not part of the result.
Exercise
A products table has columns category and brand. Write a query that returns each unique category-and-brand combination, sorted alphabetically by both columns.
Show answer
SELECT DISTINCT category, brand FROM products ORDER BY category, brand;