O'Reilly logo

SQL: Visual QuickStart Guide by Chris Fehily

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required