Finding Duplicates Using SQL

code snippet Apr 09, 2019

Suppose you have a table of users in your database.

How can you find all users that have the same email address?


SELECT email, COUNT(email) 
FROM Users(nolock)
GROUP BY email
HAVING COUNT(email) > 1

The HAVING clause is similar to the WHERE clause in SQL, except that it works with aggregate functions such as COUNT.

unsplash-logoAlicia Steels

Kristina Alberto

Software engineer from Sydney. I work at Domain Group.