The following are the ways to delete duplicate rows from a table, and its purely depends upon your environment, its recommend to first try it on development server and then move ahead to production environment.
- Using group by clause and move the records to another table, truncate original and move records back to original.
-- Create a table CREATE TABLE t1 ( col1 INT ,col2 INT ,col3 CHAR(50) ) --Insert some duplicate records INSERT INTO t1 VALUES (1,1,'Manish') INSERT INTO t1 VALUES (1,1,'Manish') INSERT INTO t1 VALUES (1,2,'Sql server') --Verify the records SELECT * FROM t1 --Fetch and save the duplicate records into temporary table SELECT * INTO t2 FROM t1 GROUP BY col1 ,col2 ,col3 HAVING COUNT(*) > 1 SELECT * FROM t2 --Delete duplicate records from original table DELETE FROM t1 FROM t1 ,t2 WHERE t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3 --Insert the delete data back INSERT INTO t1 SELECT * FROM t2 --Verify SELECT * FROM t1 --Cleanup DROP TABLE t1 DROP TABLE t2
There are still more ways to delete duplicate records, will update this post some times later.
Thanks
Manish