COUNT()

The COUNT() aggregate function returns the total number of rows that match the specified criteria.

Syntax

SELECT COUNT(column_name)
FROM table_name;

COUNT() is a function that takes the name of a column as an argument and counts the number of rows where the column is not NULL.

Note: A column name of the table can also be used instead of *. Unlike COUNT(*), this variation COUNT(column_name) will not count NULL values in that column.

Example

Suppose there’s an employees table with the following values:

name salary experience
Michael 80000 14
Pam 41500 2
Jim 45000 4
Dwight 55000 8

To find the total number of employees who have less than 5 years of experience, the given query can be used:

SELECT COUNT(*)
FROM employees
WHERE experience < 5;

The result would be:

COUNT(*)
2

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn SQL on Codecademy

Contributors