How to Delete Duplicate Records from a SQL Table Without a Primary Key
When dealing with SQL tables that lack a primary key, handling duplicate records can pose a challenge. However, there are efficient methods to eliminate duplicates based on specific criteria.
Consider the following scenario: you have a 'employee' table with columns 'EmpId,' 'EmpName,' and 'EmpSSN,' and some records contain duplicate values in the 'EmpId' and 'EmpSSN' fields. To remove these duplicates, you can utilize the following query:
DELETE SUB FROM (SELECT ROW_NUMBER() OVER (PARTITION BY EmpId, EmpName, EmpSSN ORDER BY EmpId) cnt FROM employee) SUB WHERE SUB.cnt > 1
Understanding the Query:
This query effectively removes duplicate records based on the unique combination of 'EmpId' and 'EmpSSN,' efficiently performing data cleanup in tables without a primary key.
The above is the detailed content of How to Remove Duplicate Rows from a SQL Table Without a Primary Key?. For more information, please follow other related articles on the PHP Chinese website!