DISTINCT for Removing Duplicates
What you'll learn: How to eliminate duplicate rows from SQL query results using the DISTINCT keyword.
The Problem of Duplicate Rows
When you query a database, sometimes the result set contains duplicate rows—identical records that appear multiple times. This often happens when you select only a subset of columns from a table, or when joining tables creates repeated values. DISTINCT is SQL's tool for filtering out these duplicates, ensuring each row in your result appears only once.
How DISTINCT Works
Place the DISTINCT keyword immediately after SELECT to remove duplicate rows from your results:
SELECT DISTINCT column_name FROM table_name;
SQL examines all rows in the result set and keeps only unique combinations. Think of it like a filter that screens out photocopies, keeping only one original of each document.
Single Column Example
SELECT DISTINCT city FROM customers;
If your customers table has multiple people from the same city, this returns each city name exactly once.
Multiple Columns
DISTINCT works across all specified columns together—the entire row must be identical for it to be considered a duplicate:
SELECT DISTINCT city, country FROM customers;
This removes duplicates only when both city and country match. "Paris, France" and "Paris, Texas" would both appear since the combination differs.
Important Notes
- DISTINCT applies to the entire row, not individual columns
- It adds processing overhead—SQL must compare all rows
- Works with WHERE, ORDER BY, and other clauses you've learned
- NULL values are considered equal to each other when determining uniqueness
Key Takeaway: Use DISTINCT after SELECT to eliminate duplicate rows from query results, either for single columns or unique combinations of multiple columns.