Delete duplicate SQL records

What is the easiest way to delete records with a duplicate name in a table? The answers that I came across are very confusing.

Connected:

Removing duplicate records from a table

+3
source share
2 answers

I understood! Simple and it worked great.

delete 
   t1 
from 
   tTable t1, tTable t2 
where 
   t1.locationName = t2.locationName and  
   t1.id > t2.id 

http://www.cryer.co.uk/brian/sql/sql_delete_duplicates.htm

+5
source

SQL Server 2005:

with FirstKey
AS
(
    SELECT MIN(ID), Name, COUNT(*) AS Cnt
      FROM YourTable
     GROUP BY Name
     HAVING COUNT(*) > 1
)
DELETE YourTable
  FROM YourTable YT
  JOIN FirstKey FK ON FK.Name = YT.Name AND FK.ID != YT.ID
0
source

Source: https://habr.com/ru/post/1707970/


All Articles