sqlstreak

SQL how-to

How to delete duplicate rows in SQL

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

Number the copies with ROW_NUMBER() OVER (PARTITION BY <what makes a duplicate> ORDER BY <which copy to keep>), then delete every row numbered above 1 by its id. Run the same query as a SELECT first to see what will go.

The task: Remove duplicate rows from a table and keep exactly one copy of each.

First, preview what would be deleted

Keep each person's latest request; these are the older copies.

SELECT id, email, requested_on
FROM (SELECT id, email, requested_on,
        ROW_NUMBER() OVER (
          PARTITION BY LOWER(TRIM(email))
          ORDER BY requested_on DESC, id DESC) AS rn
      FROM adoption_requests) t
WHERE rn > 1
ORDER BY id;

Result (PostgreSQL and SQLite, identical): 4 rows

idemailrequested_on
1ana@mail.com2026-10-01
2BEN@MAIL.COM2026-10-02
4cara@mail.com2026-10-06
9gus@mail.com2026-10-15

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    PARTITION BY LOWER(TRIM(email))

    Rows with the same cleaned email are copies of each other.

  2. 2
    ORDER BY requested_on DESC, id DESC

    Decides which copy gets number 1, the one you keep: the latest request, with id breaking ties.

  3. 3
    WHERE rn > 1

    Every copy after the first.

  4. 4
    DELETE … WHERE id IN (…)

    Delete those rows by their primary key.

More examples

Then delete them

The same numbering inside WHERE id IN (…). Runs here are rolled back, so the sample data is safe.

DELETE FROM adoption_requests
WHERE id IN (
  SELECT id FROM (
    SELECT id, ROW_NUMBER() OVER (
      PARTITION BY LOWER(TRIM(email))
      ORDER BY requested_on DESC, id DESC) AS rn
    FROM adoption_requests) t
  WHERE rn > 1);
SELECT id, email, requested_on FROM adoption_requests ORDER BY id;

Result (PostgreSQL and SQLite, identical): 8 rows

idemailrequested_on
3Ana@Mail.com2026-10-05
5dev@mail.com2026-10-07
6cara@mail.com2026-10-09
7eli@mail.com2026-10-10
8fay@mail.com2026-10-12
10ben@mail.com2026-10-16
11hana@mail.com2026-10-18
12GUS@mail.com 2026-10-20

Try it: PostgreSQL playground · SQLite playground

Shorter: keep the lowest id

If any copy will do, keep the smallest id of each group.

DELETE FROM adoption_requests
WHERE id NOT IN (SELECT MIN(id) FROM adoption_requests
                 GROUP BY LOWER(TRIM(email)));
SELECT COUNT(*) AS rows_left FROM adoption_requests;

Result (PostgreSQL and SQLite, identical): 1 row

rows_left
8

Try it: PostgreSQL playground · SQLite playground

Variations

No id column?

Every row still has a hidden address: ctid in PostgreSQL, rowid in SQLite. Both statements below leave one row per email.

PostgreSQL

DELETE FROM adoption_requests a
USING adoption_requests b
WHERE LOWER(TRIM(a.email)) = LOWER(TRIM(b.email))
  AND a.ctid > b.ctid;
SELECT COUNT(*) AS rows_left FROM adoption_requests;

SQLite

DELETE FROM adoption_requests
WHERE rowid NOT IN (SELECT MIN(rowid) FROM adoption_requests
                    GROUP BY LOWER(TRIM(email)));
SELECT COUNT(*) AS rows_left FROM adoption_requests;

Result (PostgreSQL and SQLite, identical): 1 row

rows_left
8

Try it: PostgreSQL playground · SQLite playground

Stop new duplicates

After cleaning, add a UNIQUE constraint (or a unique index on the cleaned value) so the database refuses the next copy.

Common mistakes

  • Deleting without a preview: run the inner query as a SELECT first.

  • ROW_NUMBER without a tie-breaker in ORDER BY: which copy survives becomes arbitrary.

  • Forgetting to normalise text: Ana@Mail.com and ana@mail.com aren't seen as copies.

PostgreSQL and SQLite

The ROW_NUMBER approach is the same in both. Without a primary key, PostgreSQL tells copies apart by ctid and SQLite by rowid, and only PostgreSQL has DELETE … USING; the variation above shows each.

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