SQL DISTINCT
Collapses duplicate rows in the result down to one — the raw table itself is never changed.
What & Why
DISTINCT removes duplicate rows from a query's result. "Duplicate" means the exact combination of every selected column matches — not just one column looking similar.
See How It Works
BUSINESS QUESTION
Spring Launch and Enterprise Search both use google_ads — watch DISTINCT collapse that duplicate channel value.
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
Watch the duplicate collapseStep 1 of 3
-- SELECT channel (no DISTINCT yet)| name | channel |
|---|---|
| Spring Launch | google_ads |
| Retention Webinar | |
| Finance Retargeting | |
| Enterprise Search | google_ads |
All four campaign rows are visible, with only the channel column selected for the result.
EXAMPLE QUERY
SELECT DISTINCT channel
FROM marketing.campaigns;Now You Try
Practice this concept
Marketing wants a deduplicated list of lead countries.
Available schema
marketingPrefix tables with marketing.table_name.
countrymarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario