# How to delete duplicate rows in SQL

Source: https://sqlstreak.com/sql/how-to/delete-duplicates · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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.

```sql
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 (both engines):

| id | email | requested_on |
| --- | --- | --- |
| 1 | ana@mail.com | 2026-10-01 |
| 2 | BEN@MAIL.COM | 2026-10-02 |
| 4 | cara@mail.com | 2026-10-06 |
| 9 | gus@mail.com | 2026-10-15 |

### Then delete them

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

```sql
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 (both engines):

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

### Shorter: keep the lowest id

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

```sql
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 (both engines):

| rows_left |
| --- |
| 8 |

## How it works

- `PARTITION BY LOWER(TRIM(email))`: Rows with the same cleaned email are copies of each other.
- `ORDER BY requested_on DESC, id DESC`: Decides which copy gets number 1, the one you keep: the latest request, with id breaking ties.
- `WHERE rn > 1`: Every copy after the first.
- `DELETE … WHERE id IN (…)`: Delete those rows by their primary key.

## 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:

```sql
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:

```sql
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 (both engines):

| rows_left |
| --- |
| 8 |

### 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.
