mysqldude

Removing duplicate records

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Find duplicates with GROUP BY ... HAVING (original)
original post
✓✓✓✓✓✓✓✓✓
DELETE invoices FROM invoices i INNER JOIN ... (as published, marked 'incorrect' in the post)
original post
✗ 1109✗ 1109✗ 1109✗ 1109✗ 1109✗ 1109✗ 1109✗ 1109✗ 1109
ALTER TABLE ... ADD UNIQUE INDEX with duplicates (post shows 1062)
original post
✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062
ALTER IGNORE TABLE ... ADD UNIQUE INDEX (original)
original post
⚠ 1681✗ 1064✗ 1064✗ 1064✗ 1064✓✓✓✓
Add a temporary id, delete with ROW_NUMBER(), add UNIQUE (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Copy DISTINCT rows to a new table and swap (works on every version)
modern rewrite
✓✓✓✓✓✓✓✓✓
Find duplicates with GROUP BY ... HAVING (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
DELETE invoices FROM invoices i INNER JOIN ... (as published, marked 'incorrect' in the post) original post
MySQL 5.6✗ 11095.7✗ 11098.0✗ 11098.4✗ 11099✗ 1109
MariaDB 10.6✗ 110910.11✗ 110911.4✗ 110911.8✗ 1109
ALTER TABLE ... ADD UNIQUE INDEX with duplicates (post shows 1062) original post
MySQL 5.6✓ 10625.7✓ 10628.0✓ 10628.4✓ 10629✓ 1062
MariaDB 10.6✓ 106210.11✓ 106211.4✓ 106211.8✓ 1062
ALTER IGNORE TABLE ... ADD UNIQUE INDEX (original) original post
MySQL 5.6⚠ 16815.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
Add a temporary id, delete with ROW_NUMBER(), add UNIQUE (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Copy DISTINCT rows to a new table and swap (works on every version) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (4)
  • DELETE invoices FROM invoices i INNER JOIN ... (as published, marked 'incorrect' in the post) (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1109: Unknown table 'invoices' in MULTI DELETE
  • ALTER IGNORE TABLE ... ADD UNIQUE INDEX (original) (MySQL 5.6): warning 1681: 'IGNORE' is deprecated and will be removed in a future release.
  • ALTER IGNORE TABLE ... ADD UNIQUE INDEX (original) (MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IGNORE TABLE invoices ADD UNIQUE INDEX (invoi
  • Add a temporary id, delete with ROW_NUMBER(), add UNIQUE (modern) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(PARTITION BY invoice_number ORDER BY tmp_id)

Related errors: 1062, 1109, 1064

The original article

First published 2012-10-31. The text below is the original; the table above shows how it behaves on current versions.

Sometimes when a database does not have a primary or unique key defined it is easy to end up with duplicate records in a table. Ensuring a correct data model is important but sometimes things can be overlooked. Now that it has happened we now need to find a way of removing these duplicate entries.

Firstly to identify a duplicate entry we need to determine what fields should be considered in determining a duplicate. For instance if we had a table invoices, we could say that the field invoice_number should be unique and we would check based on just this field. In a lab we could say that the fields batch_date and batch_number (where batch number is reset on a daily basis) represent the uniqueness of the data.

To determine the records with duplicate entries we would issue the following command:

SELECT invoice_number FROM invoices GROUP BY invoice_number HAVING COUNT(*) > 1;

or

SELECT batch_date, batch_number FROM batches GROUP BY batch_date, batch_number HAVING COUNT(*) > 1;

What this is saying is lets focus on entries that have a count per unique fields greater than 1. It is important not to check for the count per unique fields equal to 2 as there could be entries with more than 2.

Now that we can easily identify the duplicate records we now need to determine which ones should be removed. If we were to use the unique fields in a DELETE statement we would remove all entries.

-- Incorrect - this will remove all sets of duplicate records.
DELETE invoices FROM invoices i INNER JOIN (SELECT invoice_number FROM invoices GROUP BY invoice_number HAVING COUNT(*) > 1) x WHERE i.invoice_number = x.invoice_number;

The removal process is handled in different ways depending on the database server. We will discuss each one:

MySQL

MySQL has built in features (ALTER IGNORE TABLE tablename ADD UNIQUE INDEX (fields)) which allows duplicates to be removed through the addition of a unique index even with duplicate entries.

By using the IGNORE keyword the index gets created with the entries removed.

ALTER IGNORE TABLE invoices ADD UNIQUE INDEX (invoice_number);
ALTER IGNORE TABLE batches ADD UNIQUE INDEX (batch_date, batch_number);

Consider the following example:

mysql> create table invoices (invoice_number int, amount int);
Query OK, 0 rows affected (0.08 sec)

mysql> insert into invoices(invoice_number, amount) values (1,10),(2,11),(2,11),(3,99);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from invoices;
+----------------+--------+
| invoice_number | amount |
+----------------+--------+
|              1 |     10 |
|              2 |     11 |
|              2 |     11 |
|              3 |     99 |
+----------------+--------+
4 rows in set (0.00 sec)

mysql> alter table invoices add unique index (invoice_number);
ERROR 1062 (23000): Duplicate entry '2' for key 1
mysql> alter ignore table invoices add unique index (invoice_number);
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 1  Warnings: 0

mysql> select * from invoices;
+----------------+--------+
| invoice_number | amount |
+----------------+--------+
|              1 |     10 |
|              2 |     11 |
|              3 |     99 |
+----------------+--------+
3 rows in set (0.00 sec)

PostgreSQL

In PostgreSQL we can use the CTID which is a pseudo column in PostgreSQL which identifies the file block number and the position in the block. Duplicate records will still have a unique CTID value.

Using this knowledge we can remove the larger CTID entry.

DELETE FROM invoices WHERE CTID IN (SELECT MAX(CTID) FROM invoices GROUP BY invoice_number HAVING COUNT(*) > 1);

Now lets have a look at this in action:

rcashell=# create table invoices (invoice_number integer, amount integer);
CREATE TABLE
rcashell=# insert into invoices(invoice_number, amount) values (1,10),(2,11),(2,11),(3,99);
INSERT 0 4
rcashell=# select * from invoices;
 invoice_number | amount
----------------+--------
              1 |     10
              2 |     11
              2 |     11
              3 |     99
(4 rows)

rcashell=# DELETE FROM invoices WHERE ctid IN (SELECT MAX(ctid) FROM invoices GROUP BY invoice_number HAVING COUNT(*) > 1);
DELETE 1
rcashell=# select * from invoices;
 invoice_number | amount
----------------+--------
              1 |     10
              2 |     11
              3 |     99
(3 rows)

Oracle

We can use a similar method as was used with PostgreSQL with the use of the ROWID which is similar to CTID in postgreSQL as this identifies the datafile, the data block and position within the block.

DELETE FROM invoices WHERE ROWID IN (SELECT MAX(ROWID) FROM invoices GROUP BY invoice_number HAVING COUNT(*) > 1);

Let’s look at this in action:

SQL> create table invoices (invoice_number integer, amount integer);

Table created.

SQL> insert into invoices(invoice_number, amount) values (1,10);

1 row created.

SQL> insert into invoices(invoice_number, amount) values (2,11);

1 row created.

SQL> insert into invoices(invoice_number, amount) values (2,11);

1 row created.

SQL> insert into invoices(invoice_number, amount) values (3,99);

1 row created.

SQL> select * from invoices;

INVOICE_NUMBER	   AMOUNT
-------------- ----------
	     1	       10
	     2	       11
	     2	       11
	     3	       99

SQL> DELETE FROM invoices WHERE ROWID IN (SELECT MAX(ROWID) FROM invoices GROUP BY invoice_number HAVING COUNT(*) > 1);

1 row deleted.

SQL> select * from invoices;

INVOICE_NUMBER	   AMOUNT
-------------- ----------
	     1	       10
	     2	       11
	     3	       99