Delete all duplicates rows except one in SQL Server

In this post, I demonstrate two different SQL script to delete all duplicate rows except the one in SQL Server.

Before a few days ago, I was working on one production report and found duplicates records in some of our tables. I have worked around this and created different solution to remove all duplicate rows.
Let’s first create sample table and data:

First solution is using WITH CTE:

Second solution is using SELF JOIN:

Check your table after remove this duplicate:

As per your requirement you can use any one of this solution and before execute please check your query plan and choose best solution to delete duplicate rows in SQL Server.

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of