Find Records That Contain Duplicate Values
Let's say I have a mailing_list
table that contains all the email addresses that I want to send a mailing out to. Without a uniqueness constraint on the email
column, I can end up with multiple records containing the same email address — duplicates.
Here are a couple queries for checking to see if any duplicate records exist and which ones they are.
This is cool because it uses a window function, specifically the row_number()
window function, to assign an incrementing number to each row in the partition.
Here is another, conceptually simpler approach.
Though we cannot use a where
clause with an aggregate (count
), we can reach for a having
clause to grab only those results where we've found more than 1
— duplicates.
Last updated