SQL

Find and Remove Duplicate Rows in SQL

Identify and safely delete duplicate records from a SQL table based on one or more columns using a Common Table Expression (CTE) and ROW_NUMBER().

WITH CTE_Duplicates AS (
    SELECT
        id,
        column1,
        column2,
        ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) as rn
    FROM
        your_table
)
DELETE FROM your_table
WHERE id IN (SELECT id FROM CTE_Duplicates WHERE rn > 1);
How it works: This SQL query uses a CTE with ROW_NUMBER() to identify duplicate rows. It partitions the data by 'column1' and 'column2' (the columns that define a duplicate) and assigns a sequential row number within each partition. Any row with 'rn' greater than 1 is a duplicate, which is then targeted for deletion, keeping one unique record.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs