Menu

How to Find Duplicate Rows in MySQL Before They Break Your App
📰

How to Find Duplicate Rows in MySQL Before They Break Your App

DEV Community: database·Raja Hunain·11 days ago
#28Gp2z0O
#devtodatabase#devto#dev#email#group
Long article (522 words) — no AI summary yet.
Reading 0:00
15s threshold

Duplicate rows are the kind of bug that never announces itself. No error, no crash — just the same customer twice in a report, the same email getting a newsletter twice, and an import that fails one day because a value that was supposed to be unique suddenly is not. I ran into this on a real project, and the fix started with one short query.

The one query that finds them

If email is the column that should be unique, this is the check:

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

Enter fullscreen mode

Exit fullscreen mode

HAVING does the filtering here, not WHERE — WHERE runs before grouping and cannot see COUNT(*) . The result is a short list of every duplicated value and how many times it appears. That list alone tells you how bad the problem is.

Two details people usually miss. First, NULL values all land in one group, so ten rows with a missing email look like ten duplicates — add WHERE email IS NOT NULL unless a missing value genuinely counts.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.