Request a demo

BigQuery · Aggregate function

BigQuery COUNT

COUNT counts rows: COUNT(*) counts every row, COUNT(column) skips NULLs, and COUNT(DISTINCT column) counts unique non-NULL values exactly. Use it for row counts, completeness checks and unique users per day.

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

COUNT has three forms. COUNT(*) counts rows, including rows where every column is NULL. COUNT(expression) counts the rows where the expression is not NULL. COUNT(DISTINCT expression) counts unique non-NULL values. All three return an INT64, and all three return 0, not NULL, when there is nothing to count.

In GoogleSQL, COUNT(DISTINCT ...) is exact. That has a price: BigQuery has to track every distinct value of every group, so on billions of rows or a very high-cardinality column it uses far more slot time and memory than a plain count. When an estimate is good enough, Google recommends APPROX_COUNT_DISTINCT instead. Legacy SQL worked the other way round: its COUNT(DISTINCT) was only guaranteed exact below 1,000 distinct values by default, so numbers can shift when you migrate an old query.

COUNT also works as a window function. COUNT(*) OVER (PARTITION BY customer_id ORDER BY order_date) numbers each customer's orders as a running count, and COUNT(DISTINCT ...) OVER (PARTITION BY ...) counts unique values per partition. A windowed COUNT(DISTINCT ...) can't have an ORDER BY: BigQuery rejects it with Window ORDER BY is not allowed if DISTINCT is specified.

Syntax

COUNT(*) [OVER over_clause]

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

Arguments and return value

ArgumentDescription
*Counts every input row, whatever its values.
expressionA value of any type. Rows where it is NULL are not counted. With DISTINCT, ARRAY and STRUCT arguments are rejected.
DISTINCTCounts each distinct non-NULL value once. Strings are compared exactly, so Berlin and berlin are two values.
WHERE where_expressionOptional filter inside the call, such as COUNT(* WHERE status = 'failed'). This clause is in Preview; COUNTIF does the same for a plain condition.
over_clauseMakes COUNT a window function, for example OVER (PARTITION BY customer_id). With ORDER BY and no frame, the count runs from the start of the partition to the current row and its ties.

Returns: INT64. 0 when there are no input rows or every value is NULL.

Examples

COUNT(*), COUNT(column) and COUNT(DISTINCT column)

Two orders have no coupon, so COUNT(coupon) is 3 while COUNT(*) is 5. NULL is not a distinct value: there are two coupon codes.

WITH orders AS (
  SELECT 1 AS order_id, 'c1' AS customer_id, 'SPRING10' AS coupon UNION ALL
  SELECT 2, 'c2', NULL UNION ALL
  SELECT 3, 'c1', 'SPRING10' UNION ALL
  SELECT 4, 'c3', NULL UNION ALL
  SELECT 5, 'c2', 'VIP'
)
SELECT
  COUNT(*) AS orders,
  COUNT(coupon) AS orders_with_coupon,
  COUNT(DISTINCT customer_id) AS customers,
  COUNT(DISTINCT coupon) AS coupon_codes
FROM orders;
Result
ordersorders_with_couponcustomerscoupon_codes
5332

Daily active users

WITH events AS (
  SELECT DATE '2026-11-20' AS event_date, 'u1' AS user_id UNION ALL
  SELECT DATE '2026-11-20', 'u1' UNION ALL
  SELECT DATE '2026-11-20', 'u2' UNION ALL
  SELECT DATE '2026-11-21', 'u2' UNION ALL
  SELECT DATE '2026-11-21', 'u3' UNION ALL
  SELECT DATE '2026-11-21', 'u4'
)
SELECT
  event_date,
  COUNT(*) AS events,
  COUNT(DISTINCT user_id) AS active_users
FROM events
GROUP BY event_date
ORDER BY event_date;
Result
event_dateeventsactive_users
2026-11-2032
2026-11-2133

Distinct combinations of two columns

COUNT(DISTINCT user_id, page) is not valid, and DISTINCT doesn't accept a STRUCT. Turn the pair into one string. TO_JSON_STRING keeps the NULL page as part of the key; CONCAT returns NULL for that row, so COUNT skips it.

WITH page_views AS (
  SELECT 'u1' AS user_id, '/pricing' AS page UNION ALL
  SELECT 'u1', '/pricing' UNION ALL
  SELECT 'u1', '/docs' UNION ALL
  SELECT 'u2', '/pricing' UNION ALL
  SELECT 'u2', NULL
)
SELECT
  COUNT(*) AS page_views,
  COUNT(DISTINCT TO_JSON_STRING(STRUCT(user_id, page))) AS distinct_pairs,
  COUNT(DISTINCT CONCAT(user_id, '|', page)) AS distinct_pairs_concat
FROM page_views;
Result
page_viewsdistinct_pairsdistinct_pairs_concat
543

Running count per customer

With ORDER BY, the window counts rows from the start of the partition up to the current row. Without ORDER BY, it counts the whole partition.

WITH orders AS (
  SELECT 'c1' AS customer_id, DATE '2026-11-01' AS order_date UNION ALL
  SELECT 'c1', DATE '2026-11-05' UNION ALL
  SELECT 'c2', DATE '2026-11-02' UNION ALL
  SELECT 'c1', DATE '2026-11-09'
)
SELECT
  customer_id,
  order_date,
  COUNT(*) OVER (PARTITION BY customer_id ORDER BY order_date) AS order_number,
  COUNT(*) OVER (PARTITION BY customer_id) AS total_orders
FROM orders
ORDER BY customer_id, order_date;
Result
customer_idorder_dateorder_numbertotal_orders
c12026-11-0113
c12026-11-0523
c12026-11-0933
c22026-11-0211

Common mistakes

  • Counting a nullable column when you mean rows. COUNT(coupon) counts orders that used a coupon; use COUNT(*) for all orders.
  • Passing two columns. COUNT(DISTINCT user_id, page) fails with No matching signature for aggregate function COUNT, and COUNT(DISTINCT STRUCT(user_id, page)) fails because DISTINCT doesn't accept STRUCT or ARRAY arguments. Count TO_JSON_STRING(STRUCT(user_id, page)) instead.
  • Building a distinct key with CONCAT. If any part is NULL, the whole key is NULL and COUNT skips the row, so the count comes out too low.
  • Filtering on a count in WHERE. WHERE COUNT(*) > 1 fails with Aggregate function COUNT(*) not allowed in WHERE clause; filter groups with HAVING.
  • Adding ORDER BY to a windowed distinct count. COUNT(DISTINCT user_id) OVER (ORDER BY day) is rejected. For a running count of unique users, find each user's first day in a subquery (MIN(day) per user), then take a running count of those first days.

Cost and performance

  • An unfiltered SELECT COUNT(*) FROM table doesn't need to read any column: a dry run on a regular table reports 0 bytes processed. COUNT(column), COUNT(DISTINCT column) and any WHERE clause make BigQuery read those columns, and on-demand queries are billed for their bytes.
  • COUNT(DISTINCT ...) on a high-cardinality column such as a user or session ID is the expensive form, and on large groups it can fail with Resources exceeded. APPROX_COUNT_DISTINCT reads the same column, so it bills the same bytes, but it needs much less memory and slot time.

FAQ

How do I count distinct values in BigQuery?

Use COUNT(DISTINCT column). It is exact, skips NULLs and treats strings case-sensitively. For dashboards over very large tables, APPROX_COUNT_DISTINCT returns an estimate with less work.

What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts all rows. COUNT(column) counts the rows where that column is not NULL, so COUNT(*) - COUNT(column) is the number of NULLs in the column.

How do I count rows that match a condition?

Use COUNTIF: COUNTIF(status = 'failed'). It returns 0 for an empty input, while SUM(IF(status = 'failed', 1, 0)) returns NULL.

Is COUNT(DISTINCT) exact in BigQuery?

Yes, in GoogleSQL (standard SQL). Legacy SQL's COUNT(DISTINCT) was only guaranteed exact below 1,000 distinct values by default and had a separate EXACT_COUNT_DISTINCT function, so results can differ when you port old legacy queries.

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.