BigQuery · Window function
BigQuery DENSE_RANK
DENSE_RANK() ranks rows within a window partition without gaps: tied rows share a rank and the next distinct value gets the next integer (1, 1, 2). Use it for 'Nth highest value' queries and to number distinct values, such as the days a user was active.
DENSE_RANK gives rows with equal ORDER BY values the same rank, and the next distinct value always gets rank + 1. The highest rank in a partition therefore equals the number of distinct ORDER BY values in it. RANK skips numbers after ties instead, and ROW_NUMBER gives every row its own number.
That makes DENSE_RANK the tool for 'second highest' questions: QUALIFY DENSE_RANK() OVER (ORDER BY amount DESC) = 2 returns every row with the second-largest distinct amount, even when the largest amount appears twice. It also turns values into compact sequence numbers, for example numbering the distinct days on which a user was active.
ORDER BY is required and a window frame is not allowed. NULLs tie with each other and sort first in ascending order. Without PARTITION BY the whole input is ranked on one worker, so partition the window or reduce the rows first on large tables.
Syntax
DENSE_RANK() OVER ([PARTITION BY partition_expression [, ...]] ORDER BY expression [{ASC | DESC}] [, ...])Arguments and return value
| Argument | Description |
|---|---|
PARTITION BY partition_expression | Ranks each group separately. Optional. |
ORDER BY expression | Required. Rows with equal values in all ORDER BY expressions get the same rank. |
Returns: INT64, starting at 1 in each partition, without gaps.
Examples
Second-highest distinct amount per customer
c1's top amount appears twice. DENSE_RANK still gives 180 rank 2; with RANK it would be rank 3 and the filter would find nothing.
WITH orders AS (
SELECT 'c1' AS customer_id, 1001 AS order_id, 300 AS amount UNION ALL
SELECT 'c1', 1002, 300 UNION ALL
SELECT 'c1', 1003, 180 UNION ALL
SELECT 'c1', 1004, 95 UNION ALL
SELECT 'c2', 1005, 60 UNION ALL
SELECT 'c2', 1006, 45
)
SELECT customer_id, order_id, amount
FROM orders
QUALIFY DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY amount DESC) = 2
ORDER BY customer_id, order_id;| customer_id | order_id | amount |
|---|---|---|
| c1 | 1003 | 180 |
| c2 | 1006 | 45 |
RANK and DENSE_RANK on the same ties
WITH scores AS (
SELECT 'ana' AS player, 1020 AS points UNION ALL
SELECT 'ben', 1020 UNION ALL
SELECT 'chen', 980 UNION ALL
SELECT 'dara', 870
)
SELECT
player,
points,
RANK() OVER (ORDER BY points DESC) AS rank_with_gaps,
DENSE_RANK() OVER (ORDER BY points DESC) AS dense_rank
FROM scores
ORDER BY points DESC, player;| player | points | rank_with_gaps | dense_rank |
|---|---|---|---|
| ana | 1020 | 1 | 1 |
| ben | 1020 | 1 | 1 |
| chen | 980 | 3 | 2 |
| dara | 870 | 4 | 3 |
Number the days a user was active
Two events on November 1 share day number 1; the next active day is 2, whatever the calendar gap.
WITH events AS (
SELECT 'u1' AS user_id, DATE '2026-11-01' AS event_date UNION ALL
SELECT 'u1', DATE '2026-11-01' UNION ALL
SELECT 'u1', DATE '2026-11-04' UNION ALL
SELECT 'u1', DATE '2026-11-09' UNION ALL
SELECT 'u2', DATE '2026-11-03'
)
SELECT
user_id,
event_date,
DENSE_RANK() OVER (PARTITION BY user_id ORDER BY event_date) AS active_day_number
FROM events
ORDER BY user_id, event_date;| user_id | event_date | active_day_number |
|---|---|---|
| u1 | 2026-11-01 | 1 |
| u1 | 2026-11-01 | 1 |
| u1 | 2026-11-04 | 2 |
| u1 | 2026-11-09 | 3 |
| u2 | 2026-11-03 | 1 |
Common mistakes
- Using RANK for 'Nth highest'. After a tie at the top, RANK has no rank 2, so
= 2returns nothing. DENSE_RANK counts distinct values. - Using DENSE_RANK to deduplicate. Tied rows share rank 1, so
= 1keeps duplicates; use ROW_NUMBER to keep exactly one row. - Omitting ORDER BY.
DENSE_RANK() OVER (PARTITION BY customer_id)is rejected because the window needs an ORDER BY.
Cost and performance
- Without PARTITION BY, the whole input is sorted on one worker. Partition by the entity you rank within (customer, category, day), or rank a filtered, smaller set of rows.
FAQ
How do I find the second highest value in BigQuery?
Use QUALIFY DENSE_RANK() OVER (ORDER BY amount DESC) = 2 to get all rows with the second-largest distinct value, or add PARTITION BY for one answer per group.
What is the difference between DENSE_RANK and RANK?
Both give ties the same rank. DENSE_RANK continues with the next integer after a tie (1, 1, 2); RANK skips the numbers used by the tied rows (1, 1, 3).
How do I number a user's sessions or active days?
DENSE_RANK() OVER (PARTITION BY user_id ORDER BY event_date) returns 1 for the first active day, 2 for the second, and the same number for every event on the same day.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
