Stop 14 · SQL for Data AnalysisTopics · 2/9

Removing Duplicate Rows

Part of the SQL Roadmap

In short

Duplicates sneak in through double clicks, re-run imports and merged spreadsheets. They inflate counts and revenue, so finding and removing them safely is a core skill.

Duplicates are found with GROUP BY ... HAVING COUNT(*) > 1 and removed by keeping one row per key with ROW_NUMBER.

The jaadu sits on the floor surrounded by wedding invitation cards, pairing up identical cards and dropping the extra copies into a basket.

Think of it like this

Merging two wedding guest lists

For a wedding in Lucknow, the bride's and groom's families each send a guest list. Merged together, Shalini Aunty appears twice, once as 'shalini@mail.in' and once as 'Shalini@Mail.in'. You keep one entry per person, usually the first one written, and strike out the rest. That's exactly the ROW_NUMBER pattern.

Watch it happen

Customers ranked within each email

ravi@mail.in (id 3)
0
neha@mail.in (id 4)
1
Ravi@Mail.in (id 9)
2
zoya@mail.in (id 7)
3
ravi@mail.in (id 15)
4

1/5Five customer rows. Some share the same email, ignoring case.

The big ideas

What you need to know

Find first

GROUP BY the key HAVING COUNT(*) > 1 shows which values are duplicated and how many copies exist.

Rank with ROW_NUMBER

PARTITION BY the duplicate key and ORDER BY your 'keep' rule, such as earliest created_at.

Preview, then delete

SELECT the rn > 1 rows first and check them. Only then turn the same logic into a DELETE.

Stop it happening again

Add a UNIQUE index on the key after cleaning, so the database rejects future duplicates.

See it in code

Walk through the code

sql
1-- 1. Which emails are duplicated?2SELECT LOWER(email) AS email, COUNT(*) AS copies3FROM customers4GROUP BY LOWER(email)5HAVING COUNT(*) > 16ORDER BY copies DESC;7 8-- 2. Preview the rows that would go (keep the earliest signup)9WITH ranked AS (10  SELECT customer_id, email, created_at,11         ROW_NUMBER() OVER (12           PARTITION BY LOWER(email)13           ORDER BY created_at, customer_id14         ) AS rn15  FROM customers16)17SELECT * FROM ranked WHERE rn > 1;18 19-- 3. Delete them, then block new duplicates20DELETE FROM customers21WHERE customer_id IN (22  SELECT customer_id23  FROM (24    SELECT customer_id,25           ROW_NUMBER() OVER (26             PARTITION BY LOWER(email)27             ORDER BY created_at, customer_id28           ) AS rn29    FROM customers30  ) r31  WHERE rn > 132);33CREATE UNIQUE INDEX uq_customers_email ON customers (LOWER(email));

1/4The size of the problem: each duplicated email and its number of copies.

PostgreSQL syntax. Steps 1 to 3 also run in MySQL 8, where the extra derived table (r) avoids the 'can't specify target table' error; MySQL writes the expression index as ((LOWER(email))). Run step 3 inside BEGIN ... COMMIT so you can roll back if the count looks wrong.

Save yourself the pain

Common mistakes

Oops

Using SELECT DISTINCT * to 'remove' duplicates when rows differ in id or timestamp.

Do this instead

DISTINCT only drops rows that match in every column. Decide the real key, like email, and use ROW_NUMBER on it.

Oops

Deleting duplicate customers who still have orders pointing at them.

Do this instead

First repoint their orders to the kept customer_id with an UPDATE, then delete the extras.

Your mission

Dedupe a signup table

~25 min

Quick check

In ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at), which row gets rn = 1 for each email?

Go deeper

  1. 1Find duplicate customers by email.
  2. 2Keep only the latest record per customer using ROW_NUMBER.
  3. 3Investigate why duplicates exist before deleting anything.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge