Skip to content

Instantly share code, notes, and snippets.

@FrancoisConstant
Created October 17, 2011 03:57
Show Gist options
  • Save FrancoisConstant/1291912 to your computer and use it in GitHub Desktop.
Save FrancoisConstant/1291912 to your computer and use it in GitHub Desktop.
Find and remove duplicates in table - keeps only distinct values (without a temp table)
SELECT people_user.*
FROM people_user
LEFT OUTER JOIN (
SELECT MIN(user_id) as MinId, email
FROM people_user
GROUP BY email
) as KeepRows ON
people_user.user_id = KeepRows.MinId
WHERE
KeepRows.MinId IS NULL;
DELETE people_user
FROM people_user
LEFT OUTER JOIN (
SELECT MIN(user_id) as MinId, email
FROM people_user
GROUP BY email
) as KeepRows ON
people_user.user_id = KeepRows.MinId
WHERE
KeepRows.MinId IS NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment