Skip to main content
Counts the number of rows or not NULL values.

Syntax

Parameters

Valid values for the input expression include column names or functions that return a column name. When DISTINCT is being used, only the unique number of rows with no NULL values are counted. COUNT(*) returns a total count of all rows in the table, while COUNT(<column_name>) returns a count of non-null rows in the specified <column_name>.
By default, COUNT(DISTINCT) returns exact results. If you do not require a precise result and want to have faster performance, consider using the APPROX_COUNT_DISTINCT function. See below for examples and considerations.

Return Type

NUMERIC

Examples

The tournaments table contains the following data:

Rows: 5Execution time: 6.00ms

The following example counts the total number of tournament names in the tournaments table. COUNT ignores NULL values:

Rows: 1Execution time: 6.27ms

COUNT(DISTINCT) counts only unique non-NULL values, skipping duplicates. The levels table has 10 rows but only 3 distinct leveltype values (FastestLap, FirstToComplete, and Drift):

Rows: 1Execution time: 6.00ms

For large datasets, APPROX_COUNT_DISTINCT offers faster performance at the cost of a small estimation error. The following example uses 50,000 unique values to show the approximation tradeoff:

Rows: 1Execution time: 7.11ms