Friday, December 09, 2005

Finding duplicates in SQL

I found myself doing this over and over again, and I always have to look it up.

SELECT target, count(target) AS duplicates
FROM subject
GROUP BY target HAVING (duplicates>1)
*Note this is tested on MySQL but for earlier PostgreSQL might not allow you to use expression aliases in HAVING in queries.

1 comment:

Paul said...

Thanks. This helped me lots.