To delete duplicate rows in SQL while keeping the first occurrence:
- Identify the columns that define duplicates (e.g., columns like
column1
,column2
). - Use a window function like
ROW_NUMBER()
to assign a unique number to each row within the duplicate groups. - Keep only the row with the smallest identifier (e.g., the first row) and delete the other duplicate rows.
Alternatively, if the database doesn’t support window functions, use a subquery or a self-join to delete duplicates while keeping the first occurrence.
Always back up your data before running delete operations to prevent accidental data loss.