Duplicate values in any table might be due to the poor table design or unwanted data from other sources. To delete the duplicate data from the table in SQL Server, follow the below steps –
- Find duplicate rows.
- Use DELETE statement to remove the duplicate rows.
Let us create a table named Geek –
CREATE TABLE Geek( Name NVARCHAR(100) NOT NULL, Email NVARCHAR(255) NOT NULL, City NVARCHAR(100) NOT NULL);
Let us insert some values into the table Geek –
INSERT INTO Geek (Name, Email, City) VALUES
('Nisha', 'nisha@gfg.com', 'Delhi'),
('Megha', 'megha@gfg.com', 'Noida'),
('Khushi', 'khushi@gfg.com', 'Jaipur'),
('Khushi', 'khushi@gfg.com', 'Jaipur'),
('Khushi', 'khushi@gfg.com', 'Jaipur'),
('Hina', 'hina@gfg.com', 'Kanpur'),
('Hina', 'hina@gfg.com', 'Kanpur'),
('Misha', 'misha@gfg.com', 'Gurugram'),
('Misha', 'misha@gfg.com', 'Gurugram'),
('Neha', 'neha@gfg.com', 'Pilani');
Let us display the contents of table Geek –
SELECT * FROM Geek;
Table – Geek
| Name | City | |
|---|---|---|
| Nisha | nisha@gfg.com | Delhi |
| Megha | megha@gfg.com | Noida |
| Khushi | khushi@gfg.com | Jaipur |
| Khushi | khushi@gfg.com | Jaipur |
| Khushi | khushi@gfg.com | Jaipur |
| Hina | hina@gfg.com | Kanpur |
| Hina | hina@gfg.com | Kanpur |
| Hina | hina@gfg.com | Kanpur |
| Misha | misha@gfg.com | Gurugram |
| Misha | misha@gfg.com | Gurugram |
| Neha | neha@gfg.com | Pilani |
SQL Server query to delete duplicate records from the table Geek :
WITH CTE AS ( SELECT Name, Email, City ROW_NUMBER() OVER ( PARTITION BY Name, Email. City ORDER BY Name, Email. City ) row_num FROM Geek ) DELETE FROM CTE WHERE row_num > 1;
Output –
(5 rows affected)
SELECT * FROM Geek;
Table – Geek
| Name | City | |
|---|---|---|
| Nisha | nisha@gfg.com | Delhi |
| Megha | megha@gfg.com | Noida |
| Khushi | khushi@gfg.com | Jaipur |
| Hina | hina@gfg.com | Kanpur |
| Misha | misha@gfg.com | Gurugram |
| Neha | neha@gfg.com | Pilani |
Recommended Posts:
- Find Duplicates in MS SQL Server
- Delete statement in MS SQL Server
- Delete Action in MS SQL Server
- Delete Database in MS SQL Server
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | Remove Duplicates without Distinct
- SQL | DELETE Statement
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- Difference between DELETE and DROP in SQL
- SQL Server Mathematical functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
- SQL Server Identity
- SQL SERVER | Conditional Statements
- SQL Server | STUFF() Function
- SQL Server | SERVERPROPERTY()
- Mean and Mode in SQL Server
- Allow only alphabets in column in SQL Server
- SQL SERVER | IN Condition
- Reverse Statement Word by Word in SQL server
- Copy tables between databases in SQL Server using Import-and-Export Wizard
- Difference between MySQL and MS SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

