AnalystPracticeGuides
DashboardSign up

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.

Watch it solved. Q8: find duplicates (GROUP BY + HAVING COUNT), from 5:14, in our video “SQL Interview Questions and Answers for Data Analysts (Easy to Hard)”. Pressing play loads the video from YouTube (privacy). Open on YouTube

Which values are duplicated?

SELECT email, COUNT(*) AS times
FROM customers
GROUP BY email
HAVING 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 customers
WHERE 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
) t
WHERE 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.

Try it yourself. Practise this in a real database in your browser, with instant feedback.
Practise GROUP BY and HAVING Practise window functions

Updated November 2026.

← All free guides