SQL reference

SQL DISTINCT: remove duplicate rows

In Excel, you'd reach for
fxData > Remove Duplicates, or =UNIQUE()

SELECT DISTINCT shows each different row once. With one column it's a unique list; with several it's every unique combination. Nothing is deleted from the table, which makes it safer than Excel's Remove Duplicates.

Syntax

SELECT DISTINCT col_1, col_2
FROM table;
Try itEdit the query, then Run
countryacquisition_channel
Australiaorganic_search
Australiapaid_search
Australiapartner
Australiareferral
Australiasocial
Brazilorganic_search
Brazilpaid_search
Brazilpartner
Brazilreferral
Brazilsocial

Showing 10 of 15 rows. Press Run to see them all.

To actually find the duplicates (which emails appear twice?), group and count instead: SELECT email, COUNT(*) FROM customers GROUP BY email HAVING COUNT(*) > 1.

Your turn

List every different plan and mrr combination in subscriptions, cheapest first.

This page on one card

SQL DISTINCT: remove duplicate rows: the Excel way (Data > Remove Duplicates) and the SQL query, side by side
SQL DISTINCT: remove duplicate rows, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows