

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete all the duplicate records in a MySQL table?
To delete duplicate records from a table, we can use the DELETE command. Let us now create a table.
mysql> create table DuplicateDeleteDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.60 sec)
Inserting records into the table “DuplicateDeleteDemo”: Here, we have added “John” as duplicate record 3 times.
mysql> insert into DuplicateDeleteDemo values(1,'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DuplicateDeleteDemo values(1,'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DuplicateDeleteDemo values(2,'Johnson'); Query OK, 1 row affected (0.13 sec) mysql> insert into DuplicateDeleteDemo values(1,'John'); Query OK, 1 row affected (0.14 sec)
To display all records, use the SELECT statement.
mysql> select *from DuplicateDeleteDemo;
The following is the output with duplicate records.
+------+---------+ | id | name | +------+---------+ | 1 | John | | 1 | John | | 2 | Johnson | | 1 | John | +------+---------+ 4 rows in set (0.00 sec)
In the above output, we have 4 records in the table, out of which 3 records are duplicate.
To delete duplicate records, ude DELETE.
mysql> delete from DuplicateDeleteDemo where id=1; Query OK, 3 rows affected (0.19 sec)
To check if records have been deleted, let us again display all the records.
mysql> select *from DuplicateDeleteDemo;
The following output displays that all the duplicate records have been deleted.
+------+---------+ | id | name | +------+---------+ | 2 | Johnson | +------+---------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Delete all the records from a MySQL table?
- Delete all records from a table in MySQL?
- How to delete a single value from a MySQL table with duplicate records?
- How to delete all records from a table in Oracle using JDBC API?
- How to delete the duplicate values stored in reverse order from MySQL table?
- How do we count the total duplicate records in a column of MySQL table?
- Display an error while inserting duplicate records in a MySQL table
- How to convert all the records in a MySQL table from uppercase to lowercase?
- How can we delete all rows from a MySQL table?
- Delete records from a MySQL table with IN() in a single query
- Delete records from a MySQL table by excluding non-deleted records using NOT IN
- Use TRIM on all records in a MySQL table?
- How can we fetch all the records from a particular MySQL table?
- Add a single year to all the date records in a MySQL table column
- A single MySQL query to insert records (not all) in the second table from the first table
Advertisements