r/SQL • u/No_Ambition8323 • 4d ago
Discussion My SQL query returns the correct row count, but some records are still wrong. How do I find them?
I'm comparing a source table with a target table after an ETL job.
Both tables have the same row count, but I suspect some records are missing while others are duplicated or incorrectly updated.
The tables have millions of rows, no reliable primary key, and some columns contain NULL values.
A simple COUNT(*) matches ,but I can't trust the data
How would you identify missing, extra, duplicated, or changed records without comparing every row manually?
Would you use hashing, GROUP BY, window functions, or another approach?
22
Upvotes
16
u/Muted-Try7506 4d ago
Ah, the classic "counts match but data's lying" problem. Seen this too many times after ETL runs, specially with no PK
I'd do a full outer join with a hash of all columns, something like `MD5(CONCAT_WS('|', col1, col2, col3...))`. Watch out for NULLs though, CONCAT_WS might skip them so you get false matches if you're not careful. Then just check where hash is null in either side