How to Count Distinct Values in BigQuery

Last updated July 26, 2026 · By the SaturnSQL team

Use COUNT(DISTINCT column) for an exact count. For huge tables, APPROX_COUNT_DISTINCT trades a small error margin for much less compute and a much faster result.

SELECT COUNT(DISTINCT user_id) AS unique_users
FROM events;

Approximate counts for big tables

COUNT(DISTINCT ...) needs an exact shuffle of every value, which gets expensive past a few hundred million rows. APPROX_COUNT_DISTINCT uses HyperLogLog++ under the hood and returns a result within about 1-2% error, using far less compute and finishing much faster. Both scan the same bytes under on-demand pricing, so the saving shows up as speed and, under capacity-based (editions) pricing, lower slot usage, not fewer bytes billed.

SELECT APPROX_COUNT_DISTINCT(user_id) AS approx_unique_users
FROM events;

Distinct counts per group

SELECT event_type, COUNT(DISTINCT user_id) AS unique_users
FROM events
GROUP BY event_type
ORDER BY unique_users DESC;

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Related BigQuery guides