Returns the number of items in a group.
COUNT({[ALL | DISTINCT] expression] | *})
Important Distinct aggregates, for example AVG(DISTINCT column_name), COUNT(DISTINCT column_name), MAX(DISTINCT column_name), MIN(DISTINCT column_name), and SUM(DISTINCT column_name), are not supported when using CUBE or ROLLUP. If used, Microsoft® SQL Server™ returns an error message and cancels the query.
int
COUNT(*) returns the number of items in a group, including NULL values and duplicates.
COUNT(ALL expression) evaluates expression for each row in a group and returns the number of nonnull values.
COUNT(DISTINCT expression) evaluates expression for each row in a group and returns the number of unique, nonnull values.
This example finds the number of different cities in which authors live.
USE pubs
GO
SELECT COUNT(DISTINCT city)
FROM authors
GO
Here is the result set:
-----------
16
(1 row(s) affected)
This example finds the total number of books and titles.
USE pubs
GO
SELECT COUNT(*)
FROM titles
GO
Here is the result set:
-----------
18
(1 row(s) affected)
The example shows that COUNT(*) can be combined with other aggregate functions in the select list.
USE pubs
GO
SELECT COUNT(*), AVG(price)
FROM titles
WHERE advance > $1000
GO
Here is the result set:
----------- --------------------------
15 14.42
(1 row(s) affected)