To find duplicate rows in MySQL, group by the columns that define a duplicate and keep the groups with HAVING COUNT(*) > 1. That tells you which values repeat and how often. To see the full duplicate rows, join that result back to the table or use a window function.
This guide covers both approaches, how to pick the right columns, and how to prevent duplicates in the first place. Examples run on MySQL 8.4 and later.
Here a duplicate means the same email. The primary key id is always unique, so it can never be part of the duplicate definition.
Group by the column that should be unique, count the rows in each group, and keep groups with more than one.
HAVING filters after grouping, which WHERE cannot do because it runs before grouping. For a duplicate defined by several columns, list them all in both SELECT and GROUP BY:
Choose the columns carefully. If you include a column that differs between near duplicates, such as created_at, no group will have more than one row.
The grouped query gives counts only. To see every row involved, join back to the table.
This is the list you inspect before deciding which rows to keep.
MySQL 8.0 and later supports window functions. ROW_NUMBER() numbers the rows inside each duplicate group, so every row with a number above 1 is an extra copy.
The ORDER BY inside the window decides which row counts as the original: here the oldest one. This returns only the surplus copies, which is exactly what you would delete later. Always add id as a tie breaker so the result is deterministic.
On servers without window functions, a self join finds the same rows.
Each row b is returned when an older row a with the same email exists. MySQL 5.7 is end of life, so prefer the window function form on any supported version.
Values that look the same to a person may differ to MySQL. With the default case insensitive collations, [email protected] and [email protected] compare equal, but trailing spaces or invisible characters do not always. Normalize before grouping when needed:
Expressions in GROUP BY cannot use an ordinary index, so run this on large tables with care.
Finding duplicates is a cleanup step. To stop them returning, add a unique constraint once the data is clean.
After this, an insert of a repeated email fails with error 1062, and you can use INSERT ... ON DUPLICATE KEY UPDATE to handle it on purpose. Adding the index fails while duplicates still exist, so find and remove them first.
Group by the duplicate columns and use COUNT(*) with HAVING COUNT(*) > 1. To get the total number of surplus rows, subtract the number of distinct values from the number of rows.
Usually the grouping includes a column that differs between rows, such as an id or timestamp. Group only by the columns that define a duplicate.
DISTINCT returns unique rows but cannot tell you how many copies exist. GROUP BY with COUNT(*) shows the duplicate counts.
GROUP BY puts all NULL values in one group, so they count as duplicates there. A unique index, however, allows several NULL values.
Use GROUP BY with HAVING COUNT(*) > 1 to list repeated values, join back to see whole rows, and ROW_NUMBER() to mark the extra copies. Define the duplicate by the right columns, normalize text where needed, and add a unique index afterwards so the problem does not return.