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.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
Watch the duplicate collapseStep 1 of 3
-- SELECT channel (no DISTINCT yet)
namechannel
Spring Launchgoogle_ads
Retention Webinaremail
Finance Retargetinglinkedin
Enterprise Searchgoogle_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
marketing

Prefix tables with marketing.table_name.

country
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario