How to Delete Duplicate rows in Sql Table

Description:-

In this example we will see how to delete Duplicate rows from Sql table. If we want to delete duplicate multiple rows from Sql tables then Create Query in Sql and Delete from Sql tables.

First Check in table if we have Duplicate rows or not.


Then Create Sql Query and run for Delete Duplicate rows.
 
DELETE d
  FROM Customers d
    INNER JOIN (SELECT y.ContactName,y.City,y.CustomerID, ROW_NUMBER()      OVER(PARTITION BY y.ContactName,y.City
       ORDER BY y.ContactName,y.City,y.CustomerID) AS RowRank
       FROM Customers y
          INNER JOIN (SELECT ContactName,City, COUNT(*) AS CountOf
                     FROM Customers
                     GROUP BY ContactName,City
                     HAVING COUNT(*)>1
                     ) dt ON  y.ContactName=dt.ContactName and y.City=dt.City
                ) dt2 ON d.CustomerID=dt2.CustomerID
        WHERE dt2.RowRank!=1
     
Select * from Customers

Now Check in Sql Rows are deleted or not.

Related Posts

Previous
Next Post »

Thanks for comments.....