Skip to content

Instantly share code, notes, and snippets.

@WaximeA
Created August 9, 2018 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WaximeA/032753c43863362d9cce18c0460272f8 to your computer and use it in GitHub Desktop.
Save WaximeA/032753c43863362d9cce18c0460272f8 to your computer and use it in GitHub Desktop.
SQL : delete duplicate rows except one
DELETE FROM myTable
WHERE (col1, col2, col2, col3)
IN (
SELECT * FROM (
SELECT col1, col2, col2, col3
FROM myTable
GROUP BY col1, col2, col2, col3
HAVING count(*) > 1
) AS T
-- remove this where condition if you want to delete all duplicate rows
WHERE id NOT IN (
SELECT * FROM (
SELECT id
FROM myTable
GROUP BY col1, col2, col2, col3
HAVING count(*) > 1
) AS T
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment