Request a demo

BigQuery · Aggregate function

BigQuery APPROX_COUNT_DISTINCT

APPROX_COUNT_DISTINCT estimates the number of distinct non-NULL values with a HyperLogLog++ sketch. On large, high-cardinality columns it needs far less memory and slot time than COUNT(DISTINCT), at the cost of a small error. Use it for dashboards and exploration, not for billing.

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

APPROX_COUNT_DISTINCT returns an INT64 estimate of COUNT(DISTINCT expression). Instead of keeping every distinct value, BigQuery summarizes each group in a HyperLogLog++ sketch with a precision it chooses, and Google's performance guide recommends it over COUNT(DISTINCT) whenever an estimate is acceptable. Google's sketch documentation describes the typical error of a cardinality sketch as about 1% in 95% of cases. NULLs are not counted.

The sketch is thrown away after the query, and its precision can't be changed. Summing daily APPROX_COUNT_DISTINCT values to get weekly users double-counts everyone who was active on more than one day. For either need, use the HLL_COUNT functions: HLL_COUNT.INIT(x, precision) builds a sketch you can store (precision 10 to 24, default 15, which gives about ±1.15% at 95% confidence), and HLL_COUNT.MERGE combines sketches across days or segments into one distinct count.

The function reads the same column as COUNT(DISTINCT), so on-demand queries bill the same bytes. The savings are memory and slot time, plus fewer Resources exceeded failures on very large groups. It is an aggregate only: with OVER it fails with Analytic function APPROX_COUNT_DISTINCT is not supported.

Syntax

APPROX_COUNT_DISTINCT(expression [WHERE where_expression])

Arguments and return value

ArgumentDescription
expressionAny type except ARRAY, STRUCT and INTERVAL. NULLs are not counted.
WHERE where_expressionOptional filter inside the call (Preview).

Returns: INT64: an estimate of the number of distinct non-NULL values.

Examples

Exact and approximate on a small input

WITH events AS (
  SELECT 'u1' AS user_id UNION ALL
  SELECT 'u2' UNION ALL
  SELECT 'u1' UNION ALL
  SELECT 'u3' UNION ALL
  SELECT NULL
)
SELECT
  COUNT(DISTINCT user_id) AS exact_users,
  APPROX_COUNT_DISTINCT(user_id) AS approx_users
FROM events;
Result
exact_usersapprox_users
33

100,000 distinct values

On a larger input the estimate differs slightly from the exact count.

SELECT
  COUNT(DISTINCT id) AS exact_count,
  APPROX_COUNT_DISTINCT(id) AS approx_count
FROM UNNEST(GENERATE_ARRAY(1, 100000)) AS id;
Result
exact_countapprox_count
10000099651

Weekly users from daily sketches

u2 is active on both days. Adding the daily counts gives 4; merging the daily HLL++ sketches counts u2 once.

WITH events AS (
  SELECT DATE '2026-11-16' AS day, 'u1' AS user_id UNION ALL
  SELECT DATE '2026-11-16', 'u2' UNION ALL
  SELECT DATE '2026-11-17', 'u2' UNION ALL
  SELECT DATE '2026-11-17', 'u3'
),
daily AS (
  SELECT
    day,
    APPROX_COUNT_DISTINCT(user_id) AS daily_users,
    HLL_COUNT.INIT(user_id) AS users_sketch
  FROM events
  GROUP BY day
)
SELECT
  SUM(daily_users) AS sum_of_daily_counts,
  HLL_COUNT.MERGE(users_sketch) AS weekly_users
FROM daily;
Result
sum_of_daily_countsweekly_users
43

Approximate daily active users on a real table

SELECT
  DATE(event_ts) AS day,
  APPROX_COUNT_DISTINCT(user_id) AS approx_dau
FROM `project.dataset.events`
WHERE event_ts >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
GROUP BY day
ORDER BY day;

Common mistakes

  • Adding up approximate daily counts to get a weekly or monthly number. Users active on several days are counted several times. Merge HLL++ sketches instead, or count distinct over the whole period.
  • Using it where an exact number is required: invoices, reconciliation, or tests that compare two counts for equality. Use COUNT(DISTINCT) there.
  • Looking for a precision argument. APPROX_COUNT_DISTINCT has none; HLL_COUNT.EXTRACT(HLL_COUNT.INIT(x, 20)) gives a more precise estimate.
  • Expecting a lower on-demand bill. It reads the same column as COUNT(DISTINCT), so the bytes billed are identical; only slot time and memory go down.
  • Using it as a window function. APPROX_COUNT_DISTINCT(x) OVER (...) fails with Analytic function APPROX_COUNT_DISTINCT is not supported.

Cost and performance

  • Google's query performance guide recommends APPROX_COUNT_DISTINCT over COUNT(DISTINCT) when an estimate is acceptable. Approximate aggregation avoids memory that grows with the number of distinct values, which is what makes exact distinct counts slow or failing on very large groups.
  • For recurring reports, store one HLL++ sketch per day (HLL_COUNT.INIT) in a small summary table. Distinct counts for any date range then come from HLL_COUNT.MERGE over a handful of sketches instead of a rescan of the raw events.

FAQ

How accurate is APPROX_COUNT_DISTINCT?

BigQuery doesn't publish the precision it uses. Google's sketch documentation describes a typical cardinality sketch as accurate to about 1% in 95% of cases. For a known error bound, use HLL_COUNT.INIT with an explicit precision: the default of 15 gives about ±1.15% at 95% confidence.

When should I use APPROX_COUNT_DISTINCT instead of COUNT(DISTINCT)?

On large tables with many distinct values, when an estimate within a percent or so is fine: dashboards, trends, exploration. Keep COUNT(DISTINCT) for billing, finance and anything that is compared for equality.

Can I combine approximate distinct counts across days?

Not with APPROX_COUNT_DISTINCT. Build one sketch per day with HLL_COUNT.INIT and combine them with HLL_COUNT.MERGE; the merged estimate counts each user once.

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.