BigQuery · Aggregate function
BigQuery APPROX_TOP_COUNT
APPROX_TOP_COUNT(x, n) returns the n most frequent values of x with their approximate counts, as an ARRAY of STRUCT<value, count>. Use it for top pages, top error codes and skewed join keys. Unlike most aggregates it counts NULL as a value.
APPROX_TOP_COUNT finds the most frequent values of a column in one aggregate. The result is an ARRAY of up to number STRUCTs, most frequent first, each with a value field and an INT64 count field. Read it with UNNEST: SELECT top.value, top.count FROM UNNEST(top_values) AS top. The counts are estimates; when you need exact numbers, use GROUP BY value ORDER BY COUNT(*) DESC LIMIT n.
Unlike COUNT and most aggregates, APPROX_TOP_COUNT doesn't ignore NULL, so a column with many missing values often reports NULL as its top value. Filter with WHERE x IS NOT NULL if you only care about real values. When values tie at the cut-off, which of them makes the list is not defined.
A practical use is finding skewed keys before a join or GROUP BY fails. APPROX_TOP_COUNT(customer_id, 10) on the large side of a join shows at once whether one key, often NULL, 0 or 'unknown', holds a big share of the rows, a common cause of Resources exceeded. Snowflake's closest equivalent is APPROX_TOP_K; to rank values by a weight instead of by row count, BigQuery has APPROX_TOP_SUM.
Syntax
APPROX_TOP_COUNT(expression, number)Arguments and return value
| Argument | Description |
|---|---|
expression | Any groupable type. NULL is counted as a value. |
number | How many values to return. An INT64 literal or query parameter, not a column. |
Returns: ARRAY<STRUCT<value, count INT64>>, where value has the type of expression, ordered from most to least frequent. NULL when there are no input rows.
Examples
Top pages
WITH page_views AS (
SELECT page
FROM UNNEST(['/pricing', '/docs', '/pricing', '/blog', '/docs', '/pricing', '/careers', '/docs', '/pricing', '/blog']) AS page
)
SELECT top.value AS page, top.count AS views
FROM UNNEST((SELECT APPROX_TOP_COUNT(page, 3) FROM page_views)) AS top
ORDER BY views DESC;| page | views |
|---|---|
| /pricing | 4 |
| /docs | 3 |
| /blog | 2 |
NULL is counted
SELECT top.value AS country, top.count AS signups
FROM UNNEST((
SELECT APPROX_TOP_COUNT(country, 2)
FROM UNNEST(['DE', NULL, 'FR', NULL, 'DE', NULL]) AS country
)) AS top
ORDER BY signups DESC;| country | signups |
|---|---|
| NULL | 3 |
| DE | 2 |
Top status codes per service
WITH requests AS (
SELECT 'api' AS service, 500 AS status UNION ALL
SELECT 'api', 500 UNION ALL
SELECT 'api', 404 UNION ALL
SELECT 'api', 500 UNION ALL
SELECT 'web', 404 UNION ALL
SELECT 'web', 404 UNION ALL
SELECT 'web', 503
)
SELECT service, top.value AS status, top.count AS hits
FROM (
SELECT service, APPROX_TOP_COUNT(status, 2) AS top_statuses
FROM requests
GROUP BY service
), UNNEST(top_statuses) AS top
ORDER BY service, hits DESC;| service | status | hits |
|---|---|---|
| api | 500 | 3 |
| api | 404 | 1 |
| web | 404 | 2 |
| web | 503 | 1 |
Check a join key for skew
One heavy key, such as NULL or a placeholder ID, shows up at the top of the list.
SELECT APPROX_TOP_COUNT(customer_id, 10) AS heaviest_keys
FROM `project.dataset.orders`
WHERE order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY);Common mistakes
- Forgetting that NULL counts. Missing values often top the list; add
WHERE x IS NOT NULLif they are noise. - Treating
countas exact. It is an estimate; use GROUP BY with COUNT(*) for numbers that must reconcile. - Expecting a stable list when values tie at the cut-off. Ask for one or two more values than you need, or use an exact GROUP BY.
- Passing a column or expression as
number. It fails withArgument 2 to APPROX_TOP_COUNT must be a literal or query parameter. - Looking for a MODE function. BigQuery has none (
Function not found: MODE);APPROX_TOP_COUNT(x, 1)[OFFSET(0)].valueis the approximate mode.
Cost and performance
- An exact top N (
GROUP BY value ORDER BY COUNT(*) DESC LIMIT 10) has to count every distinct value first. Approximate aggregate functions avoid memory that grows with the number of distinct values, which matters on columns with millions of them. Both read the same column, so bytes billed are the same.
FAQ
How do I find the most common values in a column in BigQuery?
Exactly: SELECT x, COUNT(*) AS n FROM t GROUP BY x ORDER BY n DESC LIMIT 10. Approximately, and cheaper on huge columns: APPROX_TOP_COUNT(x, 10), read with UNNEST.
How do I get the mode in BigQuery?
There is no MODE function. Use APPROX_TOP_COUNT(x, 1)[OFFSET(0)].value for an estimate, or SELECT x FROM t GROUP BY x ORDER BY COUNT(*) DESC LIMIT 1 for the exact answer.
How do I read the value and count fields?
Unnest the array: SELECT top.value, top.count FROM UNNEST((SELECT APPROX_TOP_COUNT(page, 5) FROM page_views)) AS top. Per group, join the grouped result with UNNEST(top_values). See UNNEST.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
