SQL – How to count unique values in database

The following shows how use a simple SQL statement to create a list of unique values and a count of their occurrences from a table.

For example, if we had a table that looked like

Person_IDPerson
1Tim
2Tom
3Jim
4Bob
5Tim
6Jim
7Bob
8Bob

This SQL statement

SELECT DISTINCT Person,  COUNT(  Person ) AS Count
FROM T_Persons
GROUP BY Person

would return

PersonCount
Tim2
Tom1
Jim2
Bob3

 

Tags:

Was this article helpful?

Related Articles