Using ROW_NUMBER() and PARTITION BY (Preferred Approach)

The ROW_NUMBER() function assigns a unique row number to each record within a partition (group). We can use this to identify and delete duplicates while keeping only the required data.

Query Syntax

SQL
WITH CTE AS (
    SELECT 
        *, 
        ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) AS row_num
    FROM table_name
    WHERE condition  -- Apply filtering condition here
) 
DELETE FROM CTE 
WHERE row_num > 1;

Example

Consider a Customer table with duplicate entries based on Email.

ID Name Email City
1 John [email protected] NY
2 Jane [email protected] LA
3 John [email protected] NY
4 Sam [email protected] TX

Removing Duplicates While Keeping the First Entry.

SQL
;WITH CTE AS (
    SELECT ID 
    FROM (
        SELECT ID, 
               ROW_NUMBER() OVER (PARTITION BY NAME, Email, City ORDER BY ID) AS RN  
        FROM Customers 
        WHERE City = 'NY'  -- Only NY state filtering condition
    ) AS sub
    WHERE RN > 1
)
DELETE FROM Customers 
WHERE ID IN (SELECT ID FROM CTE);

Explanation of the Query

Please comment on your ideas, and follow for more.