COUNT function

Updated at:

COUNT is an aggregate function that returns the number of non-NULL values in a specified column. Use COUNT(*) to count all rows returned by a query, or COUNT(column) to count non-NULL values in a specific column.

Syntax

COUNT(*)
COUNT(expr)
FormDescription
COUNT(*)Counts all rows, including rows with NULL values
COUNT(expr)Counts rows where expr evaluates to a non-NULL value

Examples

The following examples run against a test table and show how COUNT(*) and COUNT(column) behave differently with NULL values.

Count all rows in a table:

SELECT COUNT(*) FROM test;

Count rows that match a condition:

SELECT COUNT(*) FROM test WHERE c1 = 1;

Count non-NULL values in a column:

SELECT COUNT(c1) FROM test WHERE c1 = 1;

This query first filters rows where c1 = 1, then counts only the rows where c1 is not NULL.