Skip to content

Instantly share code, notes, and snippets.

@dncpax
Created May 24, 2019 15:32
Show Gist options
  • Save dncpax/df64dd4fc67f1d846ba10e5a6d3ec335 to your computer and use it in GitHub Desktop.
Save dncpax/df64dd4fc67f1d846ba10e5a6d3ec335 to your computer and use it in GitHub Desktop.
postgresql - find duplicates
select * from (
SELECT id,
ROW_NUMBER() OVER(PARTITION BY column1, column2 ORDER BY id asc) AS Row
FROM tbl
) dups
where
dups.Row > 1
@dncpax
Copy link
Author

dncpax commented May 24, 2019

original here: https://stackoverflow.com/questions/14471179/find-duplicate-rows-with-postgresql/14471928#14471928

best alternative yet for me, in readability and performance, because it supports several fields and lists duplicate records except the "original" (dups.Row=1), so you can easily identify or delete the duplicates.

@dncpax
Copy link
Author

dncpax commented May 24, 2019

Another possibility I like:

select objectid, count(*)
from table where condition=value
group by objectid
HAVING count(*) > 1

Simple to read and remember...

original here: https://stackoverflow.com/questions/50805439/postgresql-fastest-way-to-get-duplicates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment