Skip to main content
Returns the number of records that meet the specified condition or 0 if no records satisfy the condition. This function does not support DISTINCT, for example count_if(DISTINCT x). This function is internally transformed to COUNT + IF:
  • Before: COUNT_IF(x)
  • After: COUNT(IF(x, 1, NULL))

Syntax

Parameters

condition: the condition that should evaluate to a BOOLEAN value. You can specify multiple count_if conditions in one SELECT statement.

Return value

Returns a numeric value. If no records can be found, 0 is returned. This function ignores NULL values.

Examples

Suppose there is a table named test_count_if.
Example 1: Count the number of rows in table test_count_if where v2 is null.
Example 2: Count the number of rows where v1 >= v2 or v4 = 1
Example 3: Specify multiple conditions.