Duplicates are the second most common SQL interview topic, and a very common real job. Customer lists, imported files and forms that were submitted twice all create them. The interview question usually has three parts: find the duplicates, show which copy to keep, and delete the others without deleting everything. We will use the contacts table, where three rows share the email asha@example.com and two share ben@example.com.
Step 1: find which values repeat
Group by the column that should be unique, count the rows in each group, and keep only the groups with more than one row. WHERE cannot do this because it works on single rows before grouping. HAVING works on groups.
SELECT email, COUNT(*) AS copies
FROM contacts
GROUP BY email
HAVING COUNT(*) > 1;
email | copies
asha@example.com | 3
ben@example.com | 2
This tells you which emails are duplicated and how many copies exist, but not which rows. That is enough for a report. For a clean-up you need the rows.
Step 2: number the copies
ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) restarts at 1 for each email and counts up in the order of the id. The row with number 1 is the first copy, which we will keep. All the others, number 2 and higher, are extra.
WITH numbered AS (
SELECT id, name, email,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn
FROM contacts
)
SELECT id, name, email, rn
FROM numbered
ORDER BY email, rn;
id | name | email | rn
1 | Asha | asha@example.com | 1
3 | Asha R | asha@example.com | 2
6 | Asha R2 | asha@example.com | 3
2 | Ben | ben@example.com | 1
5 | Ben B | ben@example.com | 2
4 | Cara | cara@example.com | 1
You can see the plan: keep ids 1, 2 and 4, and remove ids 3, 5 and 6. Showing this table to the interviewer before you delete anything is a strong habit. It proves you looked before you cut.
Step 3: delete, keeping one
The simple and portable way is to keep the smallest id in each group and delete every id that is not one of those.
DELETE FROM contacts
WHERE id NOT IN (
SELECT MIN(id) FROM contacts GROUP BY email
);
SELECT id, name, email FROM contacts ORDER BY id;
id | name | email
1 | Asha | asha@example.com
2 | Ben | ben@example.com
4 | Cara | cara@example.com
The inner query returns one id per email, the smallest. The outer DELETE removes everything else. After it runs only three rows are left, one per email. Rules about which copy to keep are a business decision. You could keep the newest one with MAX(id), or the most complete one. Ask.
The count trick
A very quick check for duplicates compares two counts: all rows and distinct values. If they differ, duplicates exist. Run it before and after the delete to see the difference.
SELECT COUNT(*) AS total_rows,
COUNT(DISTINCT email) AS unique_emails
FROM contacts;
total_rows | unique_emails
6 | 3
On the original data this shows 6 rows and 3 unique emails, so three rows are extras. After the clean-up both numbers are 3.
Other engines
- In PostgreSQL you can delete with
DELETE FROM contacts WHERE ctid NOT IN (SELECT MIN(ctid) FROM contacts GROUP BY email);or useROW_NUMBERin a subquery. - MySQL does not let you select from the same table you delete from in a plain subquery, so you wrap the inner query in another subquery or join the table to itself.
- SQL Server lets you delete straight from a common table expression that contains
ROW_NUMBER.
You do not need to know all three by heart. Know the idea, number the copies and delete the ones with a number above 1, and say that the exact syntax depends on the engine.
Prevent the problem
The real fix is a UNIQUE constraint on the column, so the database refuses a second copy in the first place. After cleaning, ALTER TABLE or CREATE UNIQUE INDEX on the email column. Saying this at the end of your answer is a nice finish, because it shows that you think beyond the one-time clean-up.
Common mistakes
- Deleting with
WHERE email IN (duplicates), which removes every copy including the one you wanted to keep. - Grouping by too many columns, so that rows that differ only in a harmless way, such as name spelling, are not seen as duplicates.
- Comparing emails that differ only in capital letters or spaces. In real data, clean with
LOWER(TRIM(email))first.
Your task
- Rebuild the practice database so the duplicates are back. Find the duplicated emails.
- Change the numbering query so that the newest copy gets number 1, then decide which rows would be deleted.
- Delete the duplicates but keep the newest copy of each email, inside a transaction, and check the remaining ids.
- Write the statement that would stop new duplicates from being added.
How to check it worked
- Before the delete,
COUNT(*)is 6 andCOUNT(DISTINCT email)is 3. - When you keep the newest copy, the remaining ids are 4, 5 and 6.
- After the clean-up each email appears exactly once.