SQL Remove Duplicate Rows
Find them first (previous lesson), then use ROW_NUMBER to keep exactly one copy of each.
What & Why
Finding duplicates tells you a problem exists. Removing them needs ROW_NUMBER() PARTITION BY the duplicate-defining columns, then keeping only rn = 1 — the exact deduplication pattern from the Window Functions series.
See How It Works
BUSINESS QUESTION
Marketing wants one latest lead row per normalized email.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
EXAMPLE QUERY
WITH ranked AS (
SELECT
id AS lead_id,
LOWER(email) AS normalized_email,
created_at,
ROW_NUMBER() OVER (PARTITION BY LOWER(email) ORDER BY created_at DESC, id DESC) AS duplicate_rank
FROM marketing.leads
)
SELECT lead_id, normalized_email, created_at
FROM ranked
WHERE duplicate_rank = 1
ORDER BY normalized_email;RESULT — latest row per email
| lead_id | normalized_email | created_at |
|---|---|---|
| 301 | ana@example.com | 2024-01-21 09:10:00+00 |
| 302 | ben@example.com | 2024-01-24 12:40:00+00 |
| 303 | chloe@example.com | 2024-02-16 08:30:00+00 |
| 304 | dev@example.com | 2024-03-20 17:15:00+00 |
All representative lead emails are unique, so each row is its partition's latest.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario