The first aggregate function I am going to cover is the COUNT() function.

The SQL COUNT aggregate function is used to count the number of rows in database table, which is directed to count on it.

The SQL COUNT syntax is simple and looks like this:

SELECT COUNT ( [COLUMN NAME] ) FROM [TABLE NAME]

If you want to count the number of rows returned by the query, the easiest way to do is use the COUNT(*) expression.


EXAMPLE :

We start with a simple example which is return number of row in the table.

Table GameScores

PlayerNameDepartmentScores
JasonIT3000
IreneIT1500
JaneMarketing1000
DavidMarketing2500
PaulHR2000
JamesHR2000

SQL statement :

SELECT COUNT(*) FROM GameScores

Result:

COUNT(*)
6


EXAMPLE (WITH DISTINCT) :

When the DISTINCT is specified before the column name, all the duplicate values are removed first and then the addition is carried out. Let's say we want to select the number of departments for contestant in the table .

SQL statement :

SELECT COUNT( DISTINCT Department) FROM GameScores

Result:

COUNT( DISTINCT Department)
3