DEV Community

Ha Tuan Em
Ha Tuan Em

Posted on

Deleting duplicate value at the same column in table of sql

Sometime, the table has duplicate value as string, number or etc,... And we want to clean that data. But the data is dynamic so we can not hard code for all case of table.

fruits table

id name
1 Orange ๐ŸŠ
2 Apple ๐ŸŽ
3 Orange ๐ŸŠ
4 Pineapple๐Ÿ

In this case, we got have a simple sql like that:

DELETE
  FROM fruits a
 USING fruits b
 WHERE a.id > b.id
   AND a.name = b.name;
Enter fullscreen mode Exit fullscreen mode

Result after excute query

id name
1 Orange ๐ŸŠ
2 Apple ๐ŸŽ
4 Pineapple๐Ÿ

If several columns have the same names but the datatypes do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an EQUIJOIN.

Find out USING in this article

Enjoy your time ๐Ÿชด
Thanks for reading.


Buy me a coffee

Top comments (1)

Collapse
 
rodkammer profile image
Rodrigo Kammer

Here's another approach:

WITH Dups AS(
   SELECt [name],
          ROW_NUMBER() OVER(PARTITION BY [name]
                                ORDER BY [Id]) AS RN
      FROM dbo.fruits
)
DELETE FROM Dups WHERE RN > 1
Enter fullscreen mode Exit fullscreen mode