August 18, 2009

Find and Delete Duplicate Rows in a Table

To find duplicate rows:

SELECT column1, column2
FROM table_name
GROUP BY column1, column2
HAVING COUNT(*) > 1;


To delete duplicate rows:

DELETE FROM table_name a
WHERE ROWID > (SELECT MIN(ROWID)
FROM table_name b
WHERE a.column1 = b.column1
AND a.column2 = b.column2
);

No comments:

Post a Comment