tinmegali
6/12/2018 - 5:26 AM

MySQL DELETE Duplicate

DELETE FROM tableName
WHERE  ID NOT IN (SELECT MAX(ID)
                  FROM (SELECT * FROM tableName) AS somAlias
                  GROUP  BY colA,
                            colB,
                            colC
                  /*Even if ID is not null-able SQL Server treats MAX(ID) as potentially
                    nullable. Because of semantics of NOT IN (NULL) including the clause
                    below can simplify the plan*/
                  HAVING MAX(ID) IS NOT NULL)