Find duplicate records in MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In the realm of database management, handling duplicate records efficiently in a MySQL database is a critical task. Duplicates can lead to redundant data storage, inaccuracies in data analysis, and overall performance degradation. This article will guide you through the process of identifying duplicate records in MySQL, with technical explanations and practical examples.
Understanding Duplicates
Before diving into technical implementations, it's essential to define what constitutes a duplicate. Typically, duplicates are records in a database table that share identical values in one or more specified columns. Identifying duplicates requires careful analysis of the data structure and an understanding of which fields should be unique.
Technical Explanation and Examples
Basic Query for Finding Duplicates
The core concept in finding duplicates is the usage of the GROUP BY clause in conjunction with the HAVING clause. The GROUP BY clause aggregates rows that have the same values in specified columns into summary rows, and the HAVING clause filters those groups based on certain conditions.
In this query:
column1is the field you suspect might have duplicate values.- The
COUNT(*) > 1condition filters groups to only those having more than one occurrence.
Multi-Column Duplicate Detection
Sometimes, duplicates are not strictly confined to a single column and can span multiple columns. Here’s how you can find duplicates across multiple fields:
Finding and Deleting Duplicates
Once duplicates are identified, the next common requirement is to delete them. To do this effectively, you may first need to distinguish between the original record and the duplicates. Here’s a plan using an additional auto-increment id field (a common primary key).
In these statements:
- The
JOINis used to match all duplicates and eliminate them except one. - The condition
t1.id > t2.idensures the oldest record (with the smallest id) remains.
Using Window Functions
While MySQL started supporting window functions with version 8.0, they provide a succinct way to handle duplicates without the need for complex joins.
This query:
- Uses a Common Table Expression (CTE) for clarity.
ROW_NUMBER()function creates a unique number for each row within the partition of duplicates.- Rows having
rn > 1are identified as duplicates.
Summary
The key to managing duplicates is an understanding of your data and the fields that should remain unique. Here is a summary in the table below:
| Method | Description | Example Query |
| Single-Column Group By | Identifies duplicates within a single column. | SELECT column1, COUNT(*)... |
| Multi-Column Group By | Finds duplicates spanning multiple columns. | SELECT column1, column2, COUNT(*)... |
| Delete with Join | Deletes duplicates using self-join, preserving minimal original. | DELETE t1 FROM... JOIN ... ON ... |
| Window Functions | Utilizes ROW_NUMBER to simplify duplicate identification. | WITH RankedDuplicates AS (... |
Conclusion
Identifying and managing duplicate records in MySQL is crucial for maintaining data integrity and optimizing database performance. By employing GROUP BY with HAVING, leveraging self-joins, or utilizing window functions, you can efficiently handle duplicates in MySQL. Make sure to back up your data before performing any deletion operations to prevent potential data losses.
Related reading
- FIND_IN_SET vs IN
- Find largest document size in MongoDB
- Find most frequent value in SQL column
- Find nearest latitude/longitude with an SQL query
- Find nearest points with MySQL from points Table
- Find Oracle JDBC driver in Maven repository
- Find records from one table which don't exist in another
- Find rows that have the same value on a column in MySQL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.