Returns the number of (distinct) non-null values or the number of selected rows.
count([all | distinct] expression)
applies count to all values. all is the default.
eliminates duplicate values before count is applied. distinct is optional.
is a column name, constant, function, any combination of column names, constants, and functions connected by arithmetic or bitwise operators, or a subquery. With aggregates, an expression is usually a column name. For more information, see “Expressions”.
Finds the number of different cities in which authors live:
select count(distinct city) from authors
Lists the types in the titles table, but eliminates the types that include only one book or none:
select type from titles group by type having count(*) > 1
count, an aggregate function, finds the number of non-null values in a column. For general information about aggregate functions, see “Aggregate functions”.
When distinct is specified, count finds the number of unique non-null values. count can be used with all datatypes, including unichar, but cannot be used with text and image. Null values are ignored when counting.
count(column_name) returns a value of 0 on empty tables, on columns that contain only null values, and on groups that contain only null values.
count(*) finds the number of rows. count(*) does not take any arguments, and cannot be used with distinct. All rows are counted, regardless of the presence of null values.
When tables are being joined, include count(*) in the select list to produce the count of the number of rows in the joined results. If the objective is to count the number of rows from one table that match criteria, use count(column_name).
count() can be used as an existence check in a subquery. For example:
select * from tab where 0 < (select count(*) from tab2 where ...)
However, because count() counts all matching values, exists or in may return results faster. For example:
select * from tab where exists (select * from tab2 where ...)
SQL92 – Compliance level: Transact-SQL extension.
Any user can execute count.
Commands compute clause, group by and having clauses, select, where clause