Using COUNT(*)

Accessing and Changing Relational Data

Accessing and Changing Relational Data

Using COUNT(*)

COUNT(*) does not require an expression parameter because it does not use information about any particular column. It counts the total number of rows that meet the qualifications of the query. COUNT(*) returns the number of rows that match the search conditions specified in the query without eliminating duplicates. It counts each row separately, including rows that contain null values. This query finds the total number of books in titles:

USE pubs
SELECT COUNT(*)
FROM titles

Here is the result set:

------------------
18

(1 row(s) affected)

COUNT(*) can be combined with other aggregate functions. This query shows COUNT(*) combined with an AVG function in which both aggregate functions aggregate data only from the rows that satisfy the WHERE clause search condition:

USE pubs
SELECT COUNT(*), AVG(price)
FROM titles
WHERE advance > $1000

Here is the result set:

----------- ------ 
15          14.42                      

(1 row(s) affected)

See Also

COUNT