MySQL Find Duplicate Records

Duplicate rows are a common problem, for example the same email saved two times. This query finds them. We use a users table where the email should be unique, but it is not.

Tutorials Class - Output Window
id name email
1 Ravi ravi@mail.com
2 Sita sita@mail.com
3 Amit ravi@mail.com
4 Neha neha@mail.com
5 Sita K sita@mail.com

Find the duplicate emails

SELECT email, COUNT(*) AS total
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
Tutorials Class - Output Window
email total
ravi@mail.com 2
sita@mail.com 2

Show the full duplicate rows

SELECT *
FROM users
WHERE email IN (
  SELECT email
  FROM users
  GROUP BY email
  HAVING COUNT(*) > 1
)
ORDER BY email;

This shows rows 1, 3, 2 and 5, so you can see which records are repeated.

Note: Use the same query with more columns in GROUP BY (like name, email) when a duplicate means that all of them match.

Related: GROUP BY and HAVING and Subqueries.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.