How to Find Duplicate Records in MySQL

Duplicate rows quietly break reports, unique constraints and imports. This guide shows five ways to find duplicate records in MySQL — GROUP BY and HAVING, self-joins, subqueries, and MySQL 8 window functions — plus how to delete the extras safely.

RH Raja HunainAuthor 25 Nov 2026 8 min read 31 views
Table of Contents
Share

Duplicate records are one of the quietest problems in a database. Nothing crashes — but the same customer shows up twice in a report, a newsletter goes out twice to the same email, and one day an import fails on a value that should have been unique. Duplicates usually creep in through a double form submit, a CSV import without a unique key, or a bug that inserted the same row twice.

This guide shows five ways to find duplicate records in MySQL, from the simple GROUP BY query every developer should know to the window functions MySQL 8 added. Every example uses the same sample table.

The sample table

All examples use this customers table:

CREATE TABLE customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(255)
);

INSERT INTO customers (name, email) VALUES
    ('Sara Ahmed', 'sara@example.com'),
    ('Bilal Khan', 'bilal@example.com'),
    ('Sara Ahmed', 'sara@example.com'),
    ('Ayesha Malik', 'ayesha@example.com'),
    ('Sara Ahmed', 'sara@example.com'),
    ('Bilal Khan', 'bilal@example.com');

Sara's email appears three times, Bilal's twice.

Method 1: GROUP BY with HAVING — the classic way

Group the rows by the column that should be unique, count the rows in each group, and keep only the groups with more than one row:

SELECT email, COUNT(*) AS occurrences
FROM customers
GROUP BY email
HAVING COUNT(*) > 1;
email occurrences
sara@example.com 3
bilal@example.com 2

Two things matter here. HAVING filters groups after they are formed — WHERE cannot do this job, because it runs before grouping and cannot see COUNT(*). And this query returns the duplicated values, not the full rows — usually exactly what a first check needs.

One trap to watch for: NULL values. GROUP BY treats all NULLs as one group, so if ten rows have a NULL email, this query reports them as duplicates even though no two of them are really the same customer. Add WHERE email IS NOT NULL when a missing value should not count as a duplicate.

Method 2: Duplicates across more than one column

Sometimes a single column is not enough: a customer is only really duplicated when both the name and the email match, since two people can share a first name and family members can share an email. Group by every column that together defines uniqueness:

SELECT name, email, COUNT(*) AS occurrences
FROM customers
GROUP BY name, email
HAVING COUNT(*) > 1;

This returns two groups: Sara Ahmed with three rows and Bilal Khan with two. A row only counts as a duplicate here when both columns match. The rule is simple: put every column that defines "the same record" into the GROUP BY, and nothing else. If you include the primary key id, every group will have exactly one row and the query will find nothing — a very common beginner mistake.

Method 3: Show the full rows, not just the counts

Method 1 tells you which values are duplicated, but not which rows carry them. To see the complete records — including their ids, so you can decide which copy to keep — join the table back to the list of duplicated values:

SELECT c.id, c.name, c.email
FROM customers AS c
JOIN (
    SELECT email
    FROM customers
    WHERE email IS NOT NULL
    GROUP BY email
    HAVING COUNT(*) > 1
) AS duplicates ON duplicates.email = c.email
ORDER BY c.email, c.id;

The inner query finds the duplicated emails; the outer query returns every row that has one of them. Ordering by email and then id groups each duplicate family together, with the oldest row (lowest id) first — usually the copy you want to keep when you clean up later. This pattern works on every MySQL version, including MySQL 5.7.

Method 4: Window functions — the MySQL 8 way

MySQL 8.0 added window functions. COUNT(*) OVER (PARTITION BY email) counts the rows in each email group without collapsing them, so you keep every row and still see its duplicate count:

SELECT
    id,
    name,
    email,
    COUNT(*) OVER (PARTITION BY email) AS occurrences
FROM customers
WHERE email IS NOT NULL
ORDER BY email, id;

Every row now carries its own count. To keep only the duplicates, wrap it in a common table expression and filter:

WITH counted AS (
    SELECT
        id, name, email,
        COUNT(*) OVER (PARTITION BY email) AS occurrences
    FROM customers
    WHERE email IS NOT NULL
)
SELECT id, name, email, occurrences
FROM counted
WHERE occurrences > 1
ORDER BY email, id;

Window functions are the cleanest option when your MySQL version supports them, because the query stays a single SELECT and reads almost like plain English.

Method 5: Self-join — comparing the table with itself

A self-join matches each row against every other row in the same table — useful when you want to compare rows pairwise, for example to find duplicates where one column matches but another differs slightly:

SELECT a.id, a.name, a.email
FROM customers a
INNER JOIN customers b
    ON a.email = b.email
    AND a.id <> b.id
ORDER BY a.email, a.id;

The a.id <> b.id condition is essential: without it, every row matches itself and the query returns the whole table. Note that each duplicate appears once per other copy, so the result has more rows than the table. That is why the self-join suits investigation better than counting — prefer Method 1 for counts and Method 3 or 4 for the row list.

Which method should you use?

Goal Best method
Quick check: which values are duplicated? Method 1 — GROUP BY + HAVING
Duplicates defined by several columns Method 2 — group by all of them
Full rows, to review before deleting Method 3 — subquery join
Full rows with a count beside each one (MySQL 8+) Method 4 — window functions
Pairwise comparison of near-duplicates Method 5 — self-join

Bonus: deleting the duplicates safely

Finding duplicates is only half the job. The safe pattern keeps the lowest id in each group and deletes the rest. Back up the table first (mysqldump or CREATE TABLE customers_backup AS SELECT * FROM customers), and test on a copy before running it on real data:

DELETE c1
FROM customers c1
JOIN customers c2
    ON c1.email = c2.email
    AND c1.id > c2.id;

This deletes every row for which another row with the same email and a smaller id exists — the first copy survives, every later copy goes. Run a SELECT with the same join first to preview exactly which rows will disappear.

Stop duplicates from coming back

Cleaning up once is not enough — the permanent fix is a UNIQUE index on the column (or columns) that must stay unique:

ALTER TABLE customers ADD UNIQUE INDEX uniq_email (email);

MySQL will now reject any insert that repeats an email, with a duplicate-entry error instead of silent corruption. Add the index only after cleaning the existing duplicates, or the statement itself will fail on the rows already there. If duplicates are genuinely allowed — two family members sharing one email, for example — use a composite unique key on the columns that together must be unique, such as (name, email).

Common questions

Does GROUP BY treat NULL as a duplicate? Yes — all NULLs land in one group. Exclude them with WHERE email IS NOT NULL unless a missing value genuinely counts as a duplicate in your case.

Will these queries be slow on a large table? GROUP BY on an unindexed column sorts the whole table, so on millions of rows it can take seconds. Add an index on the column you group by and the same query usually drops to milliseconds — check with EXPLAIN whether MySQL is using the index or doing a full scan.

What if the values differ only by case or spaces? 'Sara@example.com' and 'sara@example.com ' are different strings to MySQL under most collations, so they will not group together. Normalise first — LOWER(TRIM(email)) in the GROUP BY — when your data has that kind of noise, and fix the input validation so it stops arriving.

Conclusion

Finding duplicate records in MySQL comes down to one idea: group the rows by the value that should be unique, and keep the groups with more than one member. GROUP BY with HAVING COUNT(*) > 1 answers the question in one short query; a subquery join or a MySQL 8 window function gives you the full rows when it is time to clean up; and a UNIQUE index makes sure the problem does not come back. Run the check on your own tables today — most databases that have been running for a year or more are hiding at least a few duplicates.

#MySQL #Duplicates #SQL #Database #MySQL8

Was this helpful? 0 likes Login to like & bookmark
RH

Written by

Raja Hunain

Full Stack Developer at Raja Dev Solutions — writes about real development work, the errors he faced and exactly how he fixed them, plus practical Laravel, PHP, JavaScript and MySQL tutorials.

More about the author

Comments (0)

No comments yet. Be the first to comment.

Leave a comment

Comments are moderated and appear after approval.

Keep reading

Related Articles