21.1. COUNT() Functions

There are two forms of the COUNT() function: cardinality and expression counting.

COUNT(*) returns the number of rows in a table (called the cardinality of the table, in relational terms); it is the only standard aggregate function that uses an asterisk as a parameter. This function is very useful and usually runs quite fast, since it can use system information about the table size. Remember that NULL values are also counted, because this function deals with entire rows and not column values. There is no such thing as “NULL row”—a row exists or it does not, without regard to contents.

An empty table has a COUNT(*) of zero, which makes sense. However, all of the other aggregate functions we will discuss in this section ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.