Monday, October 13, 2008

Weekly Code: Find Duplicates in SQL

Sometimes you need to check if there's duplicated rows in a SQL table, for this you can use the query:

SELECT field, COUNT(field) AS NumOccurrences
FROM checkdup
GROUP BY field
HAVING (COUNT(field) > 1)
  • checkdup: This is the table name to check.
  • field: This is the field name to check.
This will return 2x columns, the first column is the duplicated data and second column is a count of how many times its duplicated.

1 comment:

Georginius said...

It's seems easy, I'll try it