Use the HAVING keyword to count duplicates in rows of a SQL table

There are times when you want to find out duplicated values in rows of a SQL table. We are going to use the HAVING keyword to list the values that are present more than one time. Let us consider the following table with three columns and three rows:

Firstname Lastname Age
Christos Monogios 36
John Doe 25
Christos Doe 28

The following query, where ColumnName is replaced with Firstname, will give us as a result the Christos value.

SELECT ColumnName
FROM TableName
GROUP BY ColumnName
HAVING COUNT(*) > 1
comments powered by Disqus