How to Remove Duplicate Records in SQL?
Abhishek Yadav
Select an image from your device to upload
Solution:
WITH cte AS (SELECT *,ROW_NUMBER() OVER (PARTITION BY StudentName, Subject, MarksORDER BY Id) AS rnFROM StudentExams ) DELETE FROM cte WHERE rn > 1;
Explanation:
PARTITION BY groups similar rows.
PARTITION BY
ROW_NUMBER() assigns a sequence number to each row within the group.
ROW_NUMBER()
Rows with rn > 1 are duplicates and are removed.
rn > 1
To remove duplicate records in SQL, you can use a query with functions like ROW_NUMBER() to identify duplicate rows based on specific columns, then delete the extra records while keeping one unique entry. This is commonly done using a Common Table Expression (CTE) that assigns a row number to each record within a group of duplicates, allowing you to safely remove the unwanted rows without losing valid data. wheelie life