How to find duplicate records in a MySQL table?

The examples use the students table from our MySQL tutorial: id, name, city, marks, course_id.

SELECT city, COUNT(*) AS total
FROM students
GROUP BY city
HAVING COUNT(*) > 1;
Tutorials Class - Output Window
city total
Delhi 2
Mumbai 2

Delhi and Mumbai appear in two rows each. To see the full duplicate rows, join this result back with the table. To prevent duplicates in the future, add a UNIQUE constraint. See GROUP BY and HAVING.


Learn more about the similar topics:
Tutorials
MySQL Aggregate Functions
MySQL GROUP BY and HAVING
Exercises & Assignments
Write a query to show the total balance using debit and credit entries
Write a query to show the count of students by city
Write a query to show the average marks of each city
Write a query to find the cities that have more than one student
Write a query to fetch the number of students from Delhi
Interview Questions & Answers
What is the difference between WHERE and HAVING?
What are aggregate functions in MySQL?
How to find duplicate records in a MySQL table?
How to count the number of rows in a table in MySQL?