Originally published at SQL & Cloud Lab .
This post was drafted with AI assistance.
Duplicate rows creep in through double-clicked buttons, retried imports and missing constraints. Removing them in SQL Server is straightforward once you have a repeatable method: define what a duplicate is, choose which copy to keep, delete the rest safely, and then make sure it cannot happen again.
Step 1: define what a duplicate is
Rows rarely match in every column, because each usually has its own ID and timestamp. Decide which columns make two rows "the same". For an employee import, that might be EmployeeCode alone; for payroll lines, perhaps PayrollRunId , EmployeeId and PayCode .
Step 2: count the duplicates
SELECT PayrollRunId , EmployeeId , PayCode , COUNT ( * ) AS copies FROM dbo .…