Use a CTE with ROW_NUMBER() OVER (PARTITION BY <business key> ORDER BY CreatedAt DESC, Id DESC) and DELETE FROM cte WHERE rn > 1. The tie-breaker makes it deterministic. On a large table, delete in batches (DELETE TOP (5000) in a loop) to keep the log and locks small, back up or copy the rows first, and then add a unique constraint or index so duplicates can't come back.
Checks practical use of ROW_NUMBER with a CTE and safe deletion.
Last updated 23 Nov 2026.