Returns the number of (distinct) non-null values, or the number of selected rows as an integer.


count([all | distinct] expression)




  • 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).

  • You can use count 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 ...)

See also Transact-SQL Users Guide, and compute, group by and having clauses, select, where in Reference Manual: Commands


ANSI SQL – Compliance level: Transact-SQL extension.


Any user can execute count.

Related concepts