Find duplicates with GROUP BY and HAVING
Duplicate rows hide easily, like one email saved on two customer accounts. To find them, GROUP BY the column that should be unique, so every copy lands in the same group. COUNT(*) tells you how big each group is. HAVING COUNT(*) > 1 keeps only the groups with more than one row — those are your duplicates.
A tiny example
In plain English: Show me every email that appears on more than one customer, and how many times.
SELECT email, COUNT(*) AS copies
FROM customers
GROUP BY email
HAVING COUNT(*) > 1;What comes back
| copies | |
|---|---|
| amara@mail.com | 2 |
| leo@mail.com | 3 |
Every other email appears once, so its group has a count of 1 and HAVING leaves it out. Only emails saved more than once come back.
Related SQL lessons
Ready for a break?
Take on today's cozy puzzle. A fresh, bite-sized query waits every morning — no timers, no pressure.
play today's sip