MySQL find and delete duplicate rows keeping the lowest id
SQL Dasun Kalupahana 74 views
-- Preview duplicates
SELECT email, COUNT(*) AS total
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
-- Delete duplicates, keep the lowest id
DELETE u1
FROM users u1
JOIN users u2
ON u1.email = u2.email
AND u1.id > u2.id;
Finds rows that repeat a column value and deletes all but the one with the smallest id. Run the SELECT first to preview what will be removed, and back up the table before the DELETE.
Learn more on the blog
MySQL
PostgreSQL vs MySQL Performance: Real Benchmarks Compared
Last updated: 7 November 2026. See the update log at the end."Which...
8 min read
MySQL
MySQL Too Many Connections (Error 1040): Causes and Fixes
Your site suddenly shows a database error, and the log says this:ERROR...
5 min read
MySQL
Upgrade MySQL 8.0 to 8.4: What Breaks and How to Check
MySQL 8.0 reached end of life in April 2026, which means no...
7 min read