Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active July 21, 2022 16:03
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 mikehins/fbb83311dd000f5e10ee0b60fcc6eda7 to your computer and use it in GitHub Desktop.
Save mikehins/fbb83311dd000f5e10ee0b60fcc6eda7 to your computer and use it in GitHub Desktop.
# https://sqlfordevs.io/delete-duplicate-rows
WITH duplicates AS (
SELECT id, ROW_NUMBER() OVER(
PARTITION BY firstname, lastname, email
ORDER BY age DESC
) AS rownum
FROM contacts
)
DELETE contacts
FROM contacts
JOIN duplicates USING(id)
WHERE duplicates.rownum > 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment