Counting Rows with COUNT()

Use the aggregate function COUNT() to count the number of rows in a set of values. COUNT() has two forms:

  • COUNT(expr) returns the number of rows in which expr is not null.

  • COUNT(*) returns the count of all rows in a set, including nulls and duplicates.

To count non-null rows:

  • Type:
    COUNT(expr)
    
    expr is a column name, literal, or expression. The result is an integer greater than or equal to zero.

To count all rows, including nulls:

  • Type:
    COUNT(*)
    
    COUNT(*) returns an integer greater than or equal to zero. Listing 6.5 and Figure 6.5 show some queries that involve COUNT(expr) and COUNT(*). The three queries count rows in the table titles and are identical except for the WHERE clause. The row counts in the first query ...

Get SQL: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.