How to find duplicates in SQL
Group by the columns that should be unique and keep the groups with more than one row: GROUP BY email HAVING COUNT(*) > 1. This is a standard interview task and an everyday data quality check.
Which values are duplicated?
SELECT email, COUNT(*) AS timesFROM customersGROUP BY emailHAVING COUNT(*) > 1;
For duplicates across several columns, group by all of them: GROUP BY first_name, last_name, birth_date.
How do you see the full duplicate rows?
SELECT * FROM customersWHERE email IN ( SELECT email FROM customers GROUP BY email HAVING COUNT(*) > 1)ORDER BY email;
How do you keep only one of each?
Number the rows inside each group and keep the first:
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at) AS rn FROM customers) tWHERE rn = 1;
Common mistake
"Ana@Mail.com" and "ana@mail.com " look like different values. Compare LOWER(TRIM(email)) to find duplicates that only differ in case or spaces.
Practise GROUP BY and HAVING Practise window functions
Updated November 2026.