T-SQL Query remove duplicates except for original row from the table
Review

Your SQL seems the generally accepted way (Discussed Before) of deleting duplicates.


WITH cte 
     AS (SELECT id, 
                student_code, 
                exam_code, 
                mark, 
                Row_number() 
                  OVER( 
                    partition BY id, student_code, exam_code, mark 
                    ORDER BY student_code) AS rn 
         FROM   exam_merge) 
DELETE cte 
WHERE  rn &gt; 1



Optimization

I found a possible optimization using a non-clustered index. You would like to create a (non-unique) index on your key (id) with the other columns included.

CREATE NONCLUSTERED INDEX idx_exam_merge ON exam_merge
(id) 
INCLUDE (student_code, exam_code, mark);


This example shows how such index could optimize the query plan to avoid clustered index lookup.