Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find duplicate case insensitive records in Postgresql?

I have a user table with the fields id and email (among others). The email address isn’t case insensitive, a problem we want to fix, but we have some duplicate values in there (duplicate except the case, so currently we have user@example.com and User@example.com in there). I am now trying to get an overview of all the duplicate accounts, but this query is just taking forever, I had to cancel it after 5 minutes. The table has about 250.000 records.

select * from account_emailaddress ae1
where (select count(*) from account_emailaddress ae2
where LOWER(ae1.email) = LOWER(ae2.email)) > 1

I am finding plenty of examples to find literal duplicate records, but nothing for case-insensitive duplicates. Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the having clause. Should be faster than the inner clause

select lower(email) 
from test 
group by lower(email) 
having count(*)>1

DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading