sqlstreak

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

emailrequests
cara@mail.com2

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    GROUP BY email

    One group per distinct email.

  2. 2
    COUNT(*) AS requests

    How many rows each group holds.

  3. 3
    HAVING COUNT(*) > 1

    Keep 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

emailrequests
ana@mail.com2
ben@mail.com2
cara@mail.com2
gus@mail.com2

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

idemailrequested_on
1ana@mail.com2026-10-01
3Ana@Mail.com2026-10-05
2BEN@MAIL.COM2026-10-02
10ben@mail.com2026-10-16
4cara@mail.com2026-10-06
6cara@mail.com2026-10-09
9gus@mail.com2026-10-15
12GUS@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

petemailrequests
biscuitana@mail.com2
brunogus@mail.com2
lunacara@mail.com2
mochiben@mail.com2

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.com and ana@mail.com are different values until you LOWER and TRIM them.

  • 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

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.

Start free