How to delete duplicate records in Sql


Delete duplicate records in Sql

To delete duplicate records in SQL, you can use the following syntax:

DELETE FROM [table_name] WHERE [primary_key] IN (SELECT [primary_key] FROM [table_name] GROUP BY [primary_key] HAVING COUNT(*) > 1);

This statement will delete all duplicate records from the specified table, using the primary key column to identify duplicates.

For example, if you have a table named “students” with a primary key column named “id” and you want to delete all duplicate records from this table, you can use the following statement:

DELETE FROM students WHERE id IN (SELECT id FROM students GROUP BY id HAVING COUNT(*) > 1);

This statement will delete all duplicate records from the “students” table, using the “id” column as the primary key.

For example, if the “students” table contains the following records:

ID
Name
1 Alice
2 Bob
3 Carol
3 Carol

The statement above will delete the second record with an “id” of 3, leaving the table with the following records:

ID
Name
1 Alice
2 Bob
3 Carol

In this example, the statement identified all duplicate records using the “id” column as the primary key, and it deleted the duplicate records from the table.

In summary, this example shows how to use the DELETE FROM and GROUP BY statements to delete duplicate records from a table in SQL. Deleting duplicate records in SQL is a simple process that can be easily accomplished using the DELETE FROM and GROUP BY statements. This technique allows you to identify and delete all duplicate records from a table, using the primary key column to identify duplicates.

Share on: Share YogiRaj B.Ed Study Notes on twitter Share YogiRaj B.Ed Study Notes on facebook Share YogiRaj B.Ed Study Notes on WhatsApp

Suggested Posts

How to check MySql version in Ubuntu
How to check MySql version in Ubuntu

Check MySql version in Ubuntu
To check the version of MySQL installed on an Ubuntu system, you can use the following command:
mysql -V
This command will display the version of the MySQL server that is currently installed on the system.

Read full article
How to export MySql data to excel using php
How to export MySql data to excel using php

Exporting data from a MySQL database to Excel is a common task for many users, and it can be easily accomplished using PHP. PHP is a popular scripting language that is often used for web development, and it has built-in support for working with MySQL databases.

Read full article
How To Install MySQL on Ubuntu
How To Install MySQL on Ubuntu

Installing MySQL on Ubuntu is a straightforward process that can be easily accomplished using the apt package manager. Ubuntu is a popular Linux operating system that is often used for web development, and it comes with a built-in package manager that makes it easy to install and manage software packages.

Read full article
Export mysql database | Complete tutorial
Export mysql database | Complete tutorial

Exporting a MySQL database is a common task that is often performed for backup and migration purposes. MySQL provides a number of different tools and utilities that can be used to export a database, and the specific method you use will depend on your requirements and preferences.

Read full article