Delete duplicate row from the table
Sometime our database may contain most of the duplicate data which shows redundant data on the frontend and occupies unwanted memory space. In this exercise, you will learn how to remove the duplicate rows from the MySQL table.
For this, let's create a demo MySQL table 'students'. You can either use your existing table if you have OR copy paste these statements -
CREATE TABLE IF NOT EXISTS `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
INSERT INTO `students` (`id`, `first_name`, `last_name`, `email`) VALUES
(1, 'John', 'Smith', 'john@example.com'),
(2, 'Soyam', 'Mithal', 's.mithal@example.com'),
(3, 'Rohan', 'Soy', 'rohan.soy@example.com'),
(4, 'Rita', 'Smith', 'rita@example.com'),
(5, 'John', 'Smith', 'john@example.com'),
(6, 'Sayam', 'Mitra', 'sayam.mitra@example.com'),
(7, 'Shyam', 'Mishra', 'shyam@example.com'),
(8, 'Soyam', 'Mithal', 's.mithal@example.com'),
(9, 'Rohan', 'Soy', 'rohan.soy@example.com'),
(10, 'Mita', 'Dahl', 'mita@example.com');
Solutions
There are several methods to delete duplicate rows from a MySQL table.
Method 1: Keep the highest or lowest ID
Here, we have used the greater than or less than operators to delete duplicate rows from a table.
Output of the above statement
+----+------------+-----------+-------------------------+ | id | first_name | last_name | email | +----+------------+-----------+-------------------------+ | 4 | Rita | Smith | rita@example.com | | 5 | John | Smith | john@example.com | | 6 | Sayam | Mitra | sayam.mitra@example.com | | 7 | Shyam | Mishra | shyam@example.com | | 8 | Soyam | Mithal | s.mithal@example.com | | 9 | Rohan | Soy | rohan.soy@example.com | | 10 | Mita | Dahl | mita@example.com | +----+------------+-----------+-------------------------+
Output of the above statement
+----+------------+-----------+-------------------------+ | id | first_name | last_name | email | +----+------------+-----------+-------------------------+ | 1 | John | Smith | john@example.com | | 2 | Soyam | Mithal | s.mithal@example.com | | 3 | Rohan | Soy | rohan.soy@example.com | | 4 | Rita | Smith | rita@example.com | | 6 | Sayam | Mitra | sayam.mitra@example.com | | 7 | Shyam | Mishra | shyam@example.com | | 10 | Mita | Dahl | mita@example.com | +----+------------+-----------+-------------------------+
Method 2: Use DELETE JOIN Statement
Here, we have used the MySQL INNER JOIN on the same table and delete the duplicate rows.
Output of the above statement
+----+------------+-----------+-------------------------+ | id | first_name | last_name | email | +----+------------+-----------+-------------------------+ | 4 | Rita | Smith | rita@example.com | | 5 | John | Smith | john@example.com | | 6 | Sayam | Mitra | sayam.mitra@example.com | | 7 | Shyam | Mishra | shyam@example.com | | 8 | Soyam | Mithal | s.mithal@example.com | | 9 | Rohan | Soy | rohan.soy@example.com | | 10 | Mita | Dahl | mita@example.com | +----+------------+-----------+-------------------------+
Method 3: Use Temporary Table
Here, we have removed the redundant data by creating a temporary table. These processes need several steps -
STEP1: In this, first we create a copy of 'students' table
STEP2: Insert data into 'students_temp' table -
STEP3: Next, delete 'students' table -
STEP4: Rename 'students_temp' table to 'students' table -
Method 4: Use the MAX() or MIN()
Here, we have used MySQL MAX() or MIN() methods to delete redundant data.
Output of the above statement
+----+------------+-----------+-------------------------+ | id | first_name | last_name | email | +----+------------+-----------+-------------------------+ | 1 | John | Smith | john@example.com | | 2 | Soyam | Mithal | s.mithal@example.com | | 3 | Rohan | Soy | rohan.soy@example.com | | 4 | Rita | Smith | rita@example.com | | 6 | Sayam | Mitra | sayam.mitra@example.com | | 7 | Shyam | Mishra | shyam@example.com | | 10 | Mita | Dahl | mita@example.com | +----+------------+-----------+-------------------------+
Output of the above statement
need an explanation for this answer? contact us directly to get an explanation for this answer+----+------------+-----------+-------------------------+ | id | first_name | last_name | email | +----+------------+-----------+-------------------------+ | 4 | Rita | Smith | rita@example.com | | 5 | John | Smith | john@example.com | | 6 | Sayam | Mitra | sayam.mitra@example.com | | 7 | Shyam | Mishra | shyam@example.com | | 8 | Soyam | Mithal | s.mithal@example.com | | 9 | Rohan | Soy | rohan.soy@example.com | | 10 | Mita | Dahl | mita@example.com | +----+------------+-----------+-------------------------+