🧹 How to Delete Duplicate Rows in SQL Server (Step-by-Step for Beginners)

This simple guide shows you how to remove duplicate rows from a SQL Server table using a CTE (Common Table Expression).


🎯 Step 1: Understand your table

Imagine you have a table named Employees that looks like this:

EmployeeIDFirstNameLastNameEmail
1JohnDoejohn@example.com
2JohnDoejohn@example.com
3JaneSmithjane@example.com
4JaneSmithjane@example.com

Here, John Doe and Jane Smith each appear twice. We want to keep only one copy of each.


⚙️ Step 2: Find duplicates

We’ll use a helper query called a CTE to find duplicate rows:

WITH CTE AS (
    SELECT 
        EmployeeID,
        ROW_NUMBER() OVER (
            PARTITION BY FirstName, LastName, Email 
            ORDER BY EmployeeID
        ) AS rn
    FROM Employees
)
SELECT * FROM CTE;

This does the following:

EmployeeIDrn
11
22
31
42

🧹 Step 3: Delete the duplicates

WITH CTE AS (
    SELECT 
        EmployeeID,
        ROW_NUMBER() OVER (
            PARTITION BY FirstName, LastName, Email 
            ORDER BY EmployeeID
        ) AS rn
    FROM Employees
)
DELETE FROM CTE WHERE rn > 1;

This deletes all rows where rn > 1, meaning all the duplicate copies.


✅ Step 4: Check the result

SELECT * FROM Employees;

You should now see only one of each record:

EmployeeIDFirstNameLastNameEmail
1JohnDoejohn@example.com
3JaneSmithjane@example.com

💡 Notes: