Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active March 27, 2023 11:41
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/70ea21be21b00e1784971b6ee6d2708b to your computer and use it in GitHub Desktop.
Save mikehins/70ea21be21b00e1784971b6ee6d2708b to your computer and use it in GitHub Desktop.
# https://sqlfordevs.com/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