SQL find duplicates
Group by the columns that define a duplicate and keep groups with more than one row: SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1. To see the duplicate rows themselves, number them with ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) and keep rn > 1.
Syntax
SELECT col1, col2, COUNT(*) AS n FROM table GROUP BY col1, col2 HAVING COUNT(*) > 1;| Part | What it is |
|---|---|
col1, col2 | The columns that make two rows count as the same; group by all of them |
HAVING COUNT(*) > 1 | Keeps only groups with at least two rows, which is what makes them duplicates |
Examples
SELECT LOWER(TRIM(email)) AS email, COUNT(*) AS accounts
FROM signups_raw
GROUP BY LOWER(TRIM(email))
HAVING COUNT(*) > 1
ORDER BY accounts DESC, email;Result: anna@mail.com 3, chloe@mail.com 2
Raw data holds the same email with capitals and spaces; normalizing inside GROUP BY is what makes those copies match.
SELECT id, email
FROM (
SELECT id, email,
ROW_NUMBER() OVER (PARTITION BY LOWER(TRIM(email)) ORDER BY id) AS rn
FROM signups_raw
)
WHERE rn > 1;Result: Rows 2, 8 and 13: every copy after the first, with its own id
GROUP BY can only show the key and the count; ROW_NUMBER shows each duplicate row so you can inspect or delete it.
SELECT customer_id, order_date, COUNT(*) AS n
FROM orders
GROUP BY customer_id, order_date
HAVING COUNT(*) > 1;Result: No rows: no customer placed two orders on the same day
An empty result is the normal answer to a duplicate check; the query is still right.
DELETE FROM signups_raw
WHERE id NOT IN (
SELECT MIN(id) FROM signups_raw GROUP BY LOWER(TRIM(email))
);Result: Deletes rows 2, 8 and 13 and keeps the earliest account per email
Run the SELECT version first and check the count. In PostgreSQL you can also DELETE ... USING a ROW_NUMBER CTE.
Try it yourself
From Sam in Support: "Some people signed up more than once with the same email, typed differently (capitals, extra spaces). List each email that has more than one account, in lowercase with no spaces, and how many accounts it has. Most accounts first, then alphabetically." Return email and accounts.
signups_raw(id, email, country, plan, signed_up)
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
Common mistakes
- Using WHERE COUNT(*) > 1 is an error because WHERE runs before aggregation; the condition on a count belongs in HAVING.
- Grouping by only one column when two define a duplicate (name alone instead of name and date of birth) flags people who merely share a name; group by every column that matters.
- Comparing text as stored misses copies that differ by case or whitespace (Anna@Mail.com and anna@mail.com); wrap the column in LOWER(TRIM(...)) in both SELECT and GROUP BY.
- DELETE with a plain subquery on the same table can fail in MySQL (You can't specify target table for update); wrap the subquery in one more SELECT or join a derived table.
More practice: Find duplicate rows
These drills check your answer on live data, free, no account needed.
- Repeat buyersReturn the customer_id of every customer who placed more than two orders, whatever the status.
- Customers per countryFor each country, return the country and how many customers it has.
Questions
How do I find duplicate rows across all columns?
Group by every column: SELECT a, b, c, COUNT(*) FROM t GROUP BY a, b, c HAVING COUNT(*) > 1. If the table has an id column, leave it out, since the id is the one thing that differs between true duplicates.
How do I delete duplicates but keep one?
Keep the row with the smallest id per group: DELETE FROM t WHERE id NOT IN (SELECT MIN(id) FROM t GROUP BY key_columns). Without an id, number the rows with ROW_NUMBER() OVER (PARTITION BY key_columns ORDER BY some_column) in a CTE and delete where rn > 1 (PostgreSQL and SQL Server support deleting through a CTE).
Why does my duplicate check return nothing when I can see duplicates?
The values differ in a way you cannot see: trailing spaces, different case, or a date stored once as text and once as a timestamp. Normalize with LOWER and TRIM, or compare only the date part, before grouping.
Related
- GROUP BYGROUP BY collapses rows that share a value into one row per group so you can count, sum or average them. One or several columns, the must-appear rule, HAVING.
- HAVINGHAVING filters the groups made by GROUP BY using aggregate conditions such as COUNT(*) > 1 or SUM(amount) > 1000. Examples, HAVING vs WHERE, both together.
- COUNTCOUNT(*) counts rows, COUNT(column) counts non-NULL values, COUNT(DISTINCT column) counts unique values. Examples with WHERE, GROUP BY and conditional counts.
- ROW_NUMBERROW_NUMBER() OVER (ORDER BY ...) assigns 1, 2, 3 to rows; with PARTITION BY it restarts per group. Latest row per customer, remove duplicates, paginate.