# How to find duplicate rows in SQL

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


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

```sql
SELECT email, COUNT(*) AS requests
FROM adoption_requests
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY email;
```

Result (both engines):

| email | requests |
| --- | --- |
| cara@mail.com | 2 |

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

```sql
SELECT LOWER(TRIM(email)) AS email, COUNT(*) AS requests
FROM adoption_requests
GROUP BY LOWER(TRIM(email))
HAVING COUNT(*) > 1
ORDER BY email;
```

Result (both engines):

| email | requests |
| --- | --- |
| ana@mail.com | 2 |
| ben@mail.com | 2 |
| cara@mail.com | 2 |
| gus@mail.com | 2 |

### Every copy, not one row per value

A window count tells each row how many copies its group has, without collapsing the rows.

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

| id | email | 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 |

## How it works

- `GROUP BY email`: One group per distinct email.
- `COUNT(*) AS requests`: How many rows each group holds.
- `HAVING COUNT(*) > 1`: Keep only groups with more than one row: the duplicates. HAVING, not WHERE, because it tests a count.

## Variations

### Duplicates across several columns

List every column that defines “the same”: here the same person asking about the same pet.

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

| pet | email | requests |
| --- | --- | --- |
| biscuit | ana@mail.com | 2 |
| bruno | gus@mail.com | 2 |
| luna | cara@mail.com | 2 |
| mochi | ben@mail.com | 2 |

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

Same in both engines.
