How to Use DISTINCT in SQL
Use DISTINCT when you need only unique values from one or more columns. This is perfect for removing duplicates in reports, dropdown sources, filters, and quick data audits.
What you’ll build or solve
You’ll learn how to use DISTINCT in SQL to return unique rows. You’ll also know how it behaves with multiple selected columns.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when repeated values should appear only once in the result set.
Common real-world scenarios include:
- Unique countries
- One email per user
- Filter dropdown values
- Category reporting
- Data cleanup checks
This is a bad idea when duplicates are meaningful and each row should remain visible.
Prerequisites
You only need:
- A SQL table with duplicate values
- Basic
SELECTknowledge
Step-by-step instructions
Step 1: Add DISTINCT right after SELECT
Place DISTINCT before the column list.
SQL
SELECT DISTINCT country
FROM users;
This returns each country only once.
It also works across multiple columns.
SQL
SELECT DISTINCT city, country
FROM offices;
In this case, the combination of both columns must be unique.
What to look for:
DISTINCTremoves duplicate result rows- Works with one or multiple columns
- Multi-column uniqueness checks the full combination
- Great for reports and dropdown values
- Can be slower on very large datasets
Examples you can copy
Unique countries
SQL
SELECT DISTINCT country
FROM customers;
Unique status values
SQL
SELECT DISTINCT status
FROM orders;
Unique city-country pairs
SQL
SELECT DISTINCT city, country
FROM offices;
Common mistakes and how to fix them
Mistake 1: Expecting one column to stay unique in multi-column queries
What the reader might do:
SQL
SELECT DISTINCT city, country
FROM offices;
Why it breaks: city can still repeat if paired with different countries.
Corrected approach:
Select only the exact column that needs uniqueness.
SQL
SELECT DISTINCT city
FROM offices;
Mistake 2: Using DISTINCT to hide bad joins
What the reader might do:
SQL
SELECT DISTINCT users.name
FROM users
JOIN orders ON ...
Why it breaks: duplicates may actually come from an incorrect join.
Corrected approach:
Fix the join logic first.
Mistake 3: Overusing DISTINCT in large queries
What the reader might do:
SQL
SELECT DISTINCT *
FROM events;
Why it breaks: this can be expensive and may hide data quality issues.
Corrected approach:
Select only the columns that truly need uniqueness.
Troubleshooting
If duplicates still appear, check whether multiple selected columns create unique combinations.
If performance drops, reduce the selected columns.
If DISTINCT is masking join issues, inspect the join conditions.
If the result should count unique values, combine it with COUNT().
Quick recap
- Use
DISTINCTafterSELECT - It removes duplicate result rows
- Multiple columns use combined uniqueness
- Great for filters and reports
- Do not use it to hide join mistakes
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot