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 |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |