SQL how-to
How to find duplicate rows in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Group by the column (or columns) that define a duplicate and keep the groups with HAVING COUNT(*) > 1. To list every copy instead of one row per value, add COUNT(*) OVER (PARTITION BY …) and keep the rows where it is above 1.
The task: Find the values, or whole rows, that appear more than once in a table.
Duplicates by exact value
Emails that appear in more than one adoption request, exactly as typed.
SELECT email, COUNT(*) AS requests
FROM adoption_requests
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY email;Result (PostgreSQL and SQLite, identical): 1 row
| requests | |
|---|---|
| cara@mail.com | 2 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
GROUP BY emailOne group per distinct email.
- 2
COUNT(*) AS requestsHow many rows each group holds.
- 3
HAVING COUNT(*) > 1Keep only groups with more than one row: the duplicates. HAVING, not WHERE, because it tests a count.
More examples
Ignoring case and stray spaces
The same people typed their email differently. Group by a cleaned-up version and four duplicates appear instead of one.
SELECT LOWER(TRIM(email)) AS email, COUNT(*) AS requests
FROM adoption_requests
GROUP BY LOWER(TRIM(email))
HAVING COUNT(*) > 1
ORDER BY email;Result (PostgreSQL and SQLite, identical): 4 rows
| requests | |
|---|---|
| ana@mail.com | 2 |
| ben@mail.com | 2 |
| cara@mail.com | 2 |
| gus@mail.com | 2 |
Try it: PostgreSQL playground · SQLite playground
Every copy, not one row per value
A window count tells each row how many copies its group has, without collapsing the rows.
SELECT id, email, requested_on
FROM (SELECT id, email, requested_on,
COUNT(*) OVER (PARTITION BY LOWER(TRIM(email))) AS copies
FROM adoption_requests) t
WHERE copies > 1
ORDER BY LOWER(TRIM(email)), id;Result (PostgreSQL and SQLite, identical): 8 rows
| id | requested_on | |
|---|---|---|
| 1 | ana@mail.com | 2026-10-01 |
| 3 | Ana@Mail.com | 2026-10-05 |
| 2 | BEN@MAIL.COM | 2026-10-02 |
| 10 | ben@mail.com | 2026-10-16 |
| 4 | cara@mail.com | 2026-10-06 |
| 6 | cara@mail.com | 2026-10-09 |
| 9 | gus@mail.com | 2026-10-15 |
| 12 | GUS@mail.com | 2026-10-20 |
Try it: PostgreSQL playground · SQLite playground
Variations
Duplicates across several columns
List every column that defines “the same”: here the same person asking about the same pet.
SELECT LOWER(TRIM(pet_name)) AS pet, LOWER(TRIM(email)) AS email, COUNT(*) AS requests
FROM adoption_requests
GROUP BY LOWER(TRIM(pet_name)), LOWER(TRIM(email))
HAVING COUNT(*) > 1
ORDER BY pet;Result (PostgreSQL and SQLite, identical): 4 rows
| pet | requests | |
|---|---|---|
| biscuit | ana@mail.com | 2 |
| bruno | gus@mail.com | 2 |
| luna | cara@mail.com | 2 |
| mochi | ben@mail.com | 2 |
Try it: PostgreSQL playground · SQLite playground
NULLs group together
GROUP BY puts all NULLs in one group, so several rows with a missing value look like duplicates. Add WHERE email IS NOT NULL if missing doesn't mean the same.
Common mistakes
Grouping by the id or another unique column: every group has one row, so nothing shows up.
Comparing raw text:
Ana@Mail.comandana@mail.comare different values until youLOWERandTRIMthem.Using DISTINCT: it hides duplicates instead of showing which values repeat.
PostgreSQL and SQLite
Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.
Official documentation
Go deeper
- Guide: Data cleaning in SQL
- Practice: SQL data cleaning practice: 6 exercises with answers
- Lesson: Finding duplicates (Pro)
Make it stick in five minutes a day
“Finding duplicates” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.