• Sat. Nov 23rd, 2024

SQL Interview Questions

Byskkumar199650

Aug 17, 2024
sql server

Delete duplicate rows in sql Server

We will discuss deleting all duplicate rows except one from a sql server table.

 

Let me explain what we want to achieve. We will be using Employees table for this example.
Delete duplicate rows in sql

SQL Script to create Employees table
Create table Employees
(
ID int,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(50),
Salary int
)
GO

Insert into Employees values (1, ‘Mark’, ‘Hastings’, ‘Male’, 60000)
Insert into Employees values (1, ‘Mark’, ‘Hastings’, ‘Male’, 60000)
Insert into Employees values (1, ‘Mark’, ‘Hastings’, ‘Male’, 60000)
Insert into Employees values (2, ‘Mary’, ‘Lambeth’, ‘Female’, 30000)
Insert into Employees values (2, ‘Mary’, ‘Lambeth’, ‘Female’, 30000)
Insert into Employees values (3, ‘Ben’, ‘Hoskins’, ‘Male’, 70000)
Insert into Employees values (3, ‘Ben’, ‘Hoskins’, ‘Male’, 70000)
Insert into Employees values (3, ‘Ben’, ‘Hoskins’, ‘Male’, 70000)

The delete query should delete all duplicate rows except one. The output should be as shown below, after the delete query is executed.
Delete all duplicate rows except one in sql

Here is the SQL query that does the job. PARTITION BY divides the query result set into partitions.
WITH EmployeesCTE AS
(
SELECT *, ROW_NUMBER()OVER(PARTITION BY ID ORDER BY ID) AS RowNumber
FROM Employees
)
DELETE FROM EmployeesCTE WHERE RowNumber > 1

By skkumar199650

Highly skilled and dedicated Software Developer with over 5 years of experience in developing and debugging applications. Proficient in a wide range of technologies, including .NET Technology, Core Java, MS SQL Server, AWS, Liferay, Angular, Ionic, and Azure, Power BI,SSRS,SSIS. I am passionate about delivering robust and scalable software solutions that meet the highest standards. I thrive in collaborative environments, leveraging my strong analytical and problem-solving skills to drive successful outcomes.