1
Reply

How to Remove Duplicate Records in SQL?

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.

  • ROW_NUMBER() assigns a sequence number to each row within the group.

  • Rows with rn > 1 are duplicates and are removed.

    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