Request a demo

BigQuery · Aggregate function

BigQuery COUNTIF

COUNTIF counts the rows where a BOOL condition is TRUE, for example COUNTIF(status = 'failed'). Use it for conditional counts and failure rates; several COUNTIFs in one GROUP BY give side-by-side metrics. FALSE and NULL are not counted.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

COUNTIF takes a boolean expression and returns how many rows it is TRUE for. FALSE and NULL both count as no. Because every COUNTIF is independent, one GROUP BY query can return several conditional counts side by side, such as failed, slow and total runs per pipeline, without a self-join or a subquery per metric.

It is BigQuery's shorthand for SUM(CASE WHEN condition THEN 1 ELSE 0 END), with one difference: COUNTIF returns 0 on an empty input, while SUM returns NULL. PostgreSQL's COUNT(*) FILTER (WHERE ...) is not supported and fails with a syntax error. Snowflake calls the same function COUNT_IF, which doesn't exist in BigQuery.

The condition must be a BOOL. A 0/1 integer flag has to be compared explicitly (COUNTIF(is_paid = 1)). Comparisons with NULL return NULL, so COUNTIF(status != 'success') silently skips rows whose status is NULL; write COUNTIF(status IS DISTINCT FROM 'success') when NULL should count as different.

Syntax

COUNTIF([DISTINCT] expression [WHERE where_expression]) [OVER over_clause]

Arguments and return value

ArgumentDescription
expressionA BOOL expression. Rows where it is TRUE are counted; FALSE and NULL are not. Other types fail with No matching signature.
DISTINCTCounts distinct values of the condition itself, so the result is 0 or 1. Rarely useful: to count distinct users who match a condition, use COUNT(DISTINCT IF(condition, user_id, NULL)).
WHERE where_expressionOptional extra filter inside the call (Preview).
over_clauseMakes COUNTIF a window function, for example the number of failures in the last three runs.

Returns: INT64. 0 when there are no input rows or the condition is never TRUE.

Examples

Failure rate per pipeline

WITH runs AS (
  SELECT 'orders_daily' AS pipeline, 'success' AS status UNION ALL
  SELECT 'orders_daily', 'failed' UNION ALL
  SELECT 'orders_daily', 'success' UNION ALL
  SELECT 'orders_daily', 'success' UNION ALL
  SELECT 'users_sync', 'success' UNION ALL
  SELECT 'users_sync', 'failed'
)
SELECT
  pipeline,
  COUNT(*) AS runs,
  COUNTIF(status = 'failed') AS failed_runs,
  ROUND(COUNTIF(status = 'failed') / COUNT(*), 2) AS failure_rate
FROM runs
GROUP BY pipeline
ORDER BY pipeline;
Result
pipelinerunsfailed_runsfailure_rate
orders_daily410.25
users_sync210.5

NULL and negative conditions

status != 'success' is NULL for the row without a status, so that row is not counted. IS DISTINCT FROM treats NULL as a regular value.

WITH runs AS (
  SELECT 'success' AS status UNION ALL
  SELECT 'failed' UNION ALL
  SELECT NULL
)
SELECT
  COUNTIF(status != 'success') AS not_success,
  COUNTIF(status IS DISTINCT FROM 'success') AS not_success_or_null,
  COUNTIF(status IS NULL) AS missing_status
FROM runs;
Result
not_successnot_success_or_nullmissing_status
121

Events vs distinct users that match a condition

WITH events AS (
  SELECT 'u1' AS user_id, 'purchase' AS event_name UNION ALL
  SELECT 'u1', 'purchase' UNION ALL
  SELECT 'u2', 'view' UNION ALL
  SELECT 'u3', 'purchase'
)
SELECT
  COUNTIF(event_name = 'purchase') AS purchase_events,
  COUNT(DISTINCT IF(event_name = 'purchase', user_id, NULL)) AS purchasing_users
FROM events;
Result
purchase_eventspurchasing_users
32

Failures in the last three runs

A ROWS frame turns COUNTIF into a rolling count, a simple basis for an alert such as two failures in the last three runs.

WITH runs AS (
  SELECT 1 AS run_id, 'success' AS status UNION ALL
  SELECT 2, 'failed' UNION ALL
  SELECT 3, 'failed' UNION ALL
  SELECT 4, 'success' UNION ALL
  SELECT 5, 'failed'
)
SELECT
  run_id,
  status,
  COUNTIF(status = 'failed') OVER (ORDER BY run_id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS failures_last_3
FROM runs
ORDER BY run_id;
Result
run_idstatusfailures_last_3
1success0
2failed1
3failed2
4success2
5failed2

Common mistakes

  • Passing a non-BOOL column. COUNTIF(is_paid) on an INT64 flag fails with No matching signature for aggregate function COUNTIF; write COUNTIF(is_paid = 1).
  • Losing NULL rows in negative conditions. COUNTIF(status != 'success') doesn't count rows where status is NULL. Use IS DISTINCT FROM or compare IFNULL(status, '').
  • Using COUNTIF(DISTINCT condition) to count users. It counts distinct TRUE values, so it returns 1 or 0. Use COUNT(DISTINCT IF(condition, user_id, NULL)).
  • Porting COUNT(*) FILTER (WHERE ...) from PostgreSQL. BigQuery stops with Syntax error: Expected end of input; rewrite it as COUNTIF(...).
  • Dividing by a count that can be 0. COUNTIF(clicked) / COUNTIF(shown) raises a division by zero error when nothing was shown; use SAFE_DIVIDE.

Cost and performance

  • Several COUNTIFs in one GROUP BY read the table once. Running one filtered COUNT(*) query per condition scans, and on-demand bills, the same columns again for every metric.

FAQ

How do I count rows with a condition in BigQuery?

Use COUNTIF(condition), for example COUNTIF(amount > 100). Put several COUNTIFs in the same SELECT to get several conditional counts per group in one pass.

What is the difference between COUNTIF and SUM(CASE WHEN ...)?

They count the same rows. COUNTIF is shorter and returns 0 for an empty input; SUM(CASE WHEN ... THEN 1 ELSE 0 END) returns NULL in that case.

Does BigQuery support COUNT(*) FILTER (WHERE ...)?

No. Use COUNTIF(condition). BigQuery also accepts a WHERE clause inside aggregate calls, such as COUNT(* WHERE condition), which is in Preview.

How do I calculate a percentage with COUNTIF?

Divide by COUNT(*): COUNTIF(status = 'failed') / COUNT(*) returns a FLOAT64 between 0 and 1. Round it with ROUND and use SAFE_DIVIDE when the denominator can be 0.

Errors you may hit

More aggregate functions

All BigQuery aggregate functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.