MySQL
Remove Duplicates
SQL Queries
Database Management
Data Cleaning

Remove duplicate rows in MySQL

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Removing duplicate rows in a database is a crucial task for maintaining data integrity and quality. MySQL, a widely used relational database management system, provides several methods to identify and eliminate duplicate data. This article explores different techniques to remove duplicate rows in MySQL, along with technical explanations and examples.

Understanding Duplicates

Duplicates arise when the same data gets inserted more than once, either due to incorrect data entry, application errors, or other reasons. They are harmful because they can skew results, inflate costs, and complicate data operations. Therefore, it's essential to periodically clean your database by removing duplicates.

Identifying Duplicates

Before deleting duplicates, we need to identify them. Consider the table employees:

sql
1CREATE TABLE employees (
2    id INT AUTO_INCREMENT PRIMARY KEY,
3    name VARCHAR(100),
4    email VARCHAR(100),
5    department VARCHAR(50)
6);
7
8INSERT INTO employees (name, email, department) VALUES
9('Alice', '[email protected]', 'IT'),
10('Bob', '[email protected]', 'Sales'),
11('Alice', '[email protected]', 'IT'),  -- Duplicate
12('Charlie', '[email protected]', 'HR'),
13('Alice', '[email protected]', 'IT');  -- Duplicate

Finding Duplicates

To find duplicates, we can use the GROUP BY statement along with the HAVING clause:

sql
1SELECT name, email, department, COUNT(*)
2FROM employees
3GROUP BY name, email, department
4HAVING COUNT(*) > 1;

This query will count the number of times each combination of name, email, and department appears, showing only those with a count greater than one, indicating duplicates.

Removing Duplicates

There are several methods to remove duplicates in MySQL. The core idea is to retain one instance of each duplicate and remove the rest. We'll explore a few techniques:

Method 1: Using a Temporary Table

One straightforward approach is to use a temporary table:

sql
1CREATE TEMPORARY TABLE temp_employees AS
2SELECT * FROM employees
3GROUP BY name, email, department;
4
5-- Clear original table
6TRUNCATE TABLE employees;
7
8-- Reinsert distinct entries
9INSERT INTO employees (name, email, department)
10SELECT name, email, department FROM temp_employees;

In this method, we create a temporary table to hold unique records, clear the original table, and then reinsert the deduplicated data.

Method 2: Using DELETE with a Subquery

An alternative method is to use MySQL's DELETE feature combined with a subquery:

sql
1DELETE e1 FROM employees e1
2INNER JOIN employees e2 
3WHERE 
4    e1.id > e2.id AND 
5    e1.name = e2.name AND 
6    e1.email = e2.email AND 
7    e1.department = e2.department;

This query deletes duplicate rows by comparing each row (e1) with every other row (e2). If a duplicate is found, it deletes the row with the higher id.

Method 3: Using Row Number

MySQL 8.0 and later support window functions such as ROW_NUMBER(), which can also be employed for this purpose:

sql
1WITH RankedEmployees AS (
2    SELECT *,
3           ROW_NUMBER() OVER (PARTITION BY name, email, department ORDER BY id) as rn
4    FROM employees
5)
6
7DELETE FROM employees
8WHERE id IN (
9    SELECT id FROM RankedEmployees WHERE rn > 1
10);

Here, ROW_NUMBER() assigns an increasing integer value to rows within each group of duplicates, starting with 1. We then remove rows where this number is greater than 1.

Advantages and Disadvantages

Advantages of Removing Duplicates

  1. Data Integrity: Ensures consistency and correctness in datasets.
  2. Performance Improvement: Reduces the dataset size, enhancing query performance.
  3. Cost Efficiency: Decreased data redundancy can lower storage costs.

Disadvantages

  1. Data Loss Risk: If not executed carefully, valid data can be accidentally removed.
  2. Complexity: Implementation might be complicated in large or nested datasets.

Summary Table

MethodDescriptionAdvantagesDisadvantages
Temporary TableUse a temp table to store unique data and reloadSimple & clear processRequires multiple steps
Delete with SubqueryDeletes using a join and conditionsEffective for small tablesHarder to implement for complex criteria
Row NumberUtilizes window functionsPowerful and flexibleOnly available for MySQL 8.0+

Conclusion

Removing duplicate rows in MySQL is a vital task in data management. By understanding different methods — including using temporary tables, DELETE operations, and window functions — you can maintain data quality and optimize database performance. When implementing these techniques, be careful to back up your data to prevent accidental data loss.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.