How do I find which values are duplicated?
Group by the columns that should be unique and keep groups with more than one row:
SELECT email, COUNT(*) AS copies FROM customers GROUP BY email HAVING COUNT(*) > 1 ORDER BY copies DESC;
For duplicates across several columns, list them all: GROUP BY order_id, sku. This works on every MySQL and MariaDB version. On a large table, an index on the grouped columns makes it fast; without one, MySQL scans and sorts the whole table, which is fine at night and noticeable at peak.
How do I see the full duplicate rows?
Window functions, available in MySQL 8 and MariaDB 10.2 and later, number the rows inside each group:
SELECT * FROM (
SELECT c.*, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn,
COUNT(*) OVER (PARTITION BY email) AS copies
FROM customers c
) t
WHERE copies > 1
ORDER BY email, rn;
Row 1 in each group is the oldest by id. Before deleting, look at a sample: sometimes "duplicates" differ in a column that matters, such as a different address or a later status, and the newest row is the one to keep.
Are some duplicates hidden by case or spaces?
Often. With the common case-insensitive collations, Ana@Example.net and ana@example.net already group together; with binary or case-sensitive collations they do not. Trailing spaces and invisible characters cause the opposite problem. Normalize while grouping to see the real picture:
SELECT LOWER(TRIM(email)) AS norm, COUNT(*) FROM customers GROUP BY norm HAVING COUNT(*) > 1;
If this finds more groups than the plain query, clean the data with an UPDATE first, then deduplicate.
How do I delete the extra copies?
Back up the table first: mysqldump --single-transaction shop customers > /var/backups/customers-before-dedupe.sql. Then keep the lowest id in each group. Two common forms:
-- self-join, works everywhere
DELETE c1 FROM customers c1
JOIN customers c2 ON c1.email = c2.email AND c1.id > c2.id;
-- window function, MySQL 8 and MariaDB 10.2+
DELETE FROM customers WHERE id IN (
SELECT id FROM (
SELECT id, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn FROM customers
) x WHERE rn > 1
);
The extra derived table in the second form is needed because MySQL does not allow a subquery to read from the table being deleted directly. Run the matching SELECT first and compare counts; then run the delete inside a transaction so you can roll back if the number looks wrong.
How do I dedupe a table with millions of rows?
Delete in batches so locks stay short and replicas keep up:
DELETE c1 FROM customers c1 JOIN customers c2 ON c1.email = c2.email AND c1.id > c2.id WHERE c1.id BETWEEN 1 AND 200000;
Step the id range through the table. On a busy server, a script that pauses between batches keeps checkout queries fast. For tables of tens of millions of rows, creating a deduplicated copy with INSERT ... SELECT and swapping names with RENAME TABLE can be faster still. The MySQL servers guide covers sizing memory for work like this.
How do I stop duplicates from coming back?
Add a unique index once the data is clean:
ALTER TABLE customers ADD UNIQUE KEY uq_customers_email (email);
Then make the application handle the conflict: INSERT ... ON DUPLICATE KEY UPDATE for upserts, or INSERT IGNORE when a repeat should do nothing. Retries from queues and payment callbacks, the most common source of duplicates, become harmless.
What else causes duplicates?
- Imports run twice; give import batches an id and refuse repeats.
- Double clicks on submit buttons; disable the button and use idempotency keys.
- Replication to two writable primaries; keep one writer.
Finding duplicate lines in plain files, such as logs or CSV exports, is a shell job: sort file.csv | uniq -d prints repeated lines, and counting matches is covered in Count matches with grep.
Keep Reading
MySQL servers
Give InnoDB a buffer pool that holds your working set, NVMe for the redo log and no hypervisor between query and disk.
OpenKill a MySQL query or connection
Find runaway MySQL or MariaDB queries in the processlist, end the query or the whole connection, and set time limits so they stop themselves.
OpenCount matches with grep
Count matching lines, every match, matches per file and per value with grep -c, -o and a few pipes, plus common counting traps.
OpenMySQL users and privileges
Create MySQL 8 accounts with caching_sha2 passwords, least-privilege grants, roles, lockout rules and dynamic privileges for admin tasks.
OpenFAQ
Quick Answers
GROUP BY the column and add HAVING COUNT(*) > 1.
Use ROW_NUMBER() OVER (PARTITION BY column ORDER BY id) on MySQL 8 or MariaDB 10.2 and later.
Use a self-join DELETE that removes rows with a higher id than another row with the same value.
Add a UNIQUE index on the column and handle conflicts with ON DUPLICATE KEY UPDATE.
Case differences or trailing spaces may hide them. Group by LOWER(TRIM(column)) to check.