BigQuery · Window function
BigQuery RANK
RANK() assigns 1-based ranks within a window partition; tied rows share a rank and the next rank skips ahead (1, 1, 3). Use it for leaderboards and top-N queries that should keep ties, such as QUALIFY RANK() OVER (ORDER BY revenue DESC) <= 3.
RANK orders the rows of each partition and gives rows with equal ORDER BY values the same rank. After a tie the rank jumps by the number of tied rows, so two rows tied for first place are followed by rank 3. DENSE_RANK doesn't leave that gap, and ROW_NUMBER never ties.
ORDER BY is required in the OVER clause, and a window frame is not allowed. NULLs tie with each other; they sort first in ascending order and last in descending order, and NULLS LAST or NULLS FIRST in the window's ORDER BY changes that.
A top-N filter with RANK can return more than N rows: QUALIFY RANK() OVER (PARTITION BY category ORDER BY revenue DESC) <= 2 keeps every product tied for second place. That is usually what a leaderboard wants. When you need exactly N rows, use ROW_NUMBER with a tie-breaker.
Syntax
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.
Examples
Leaderboard with ties
WITH scores AS (
SELECT 'ana' AS player, 980 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
FROM scores
ORDER BY rank, player;| player | points | rank |
|---|---|---|
| ben | 1020 | 1 |
| ana | 980 | 2 |
| chen | 980 | 2 |
| dara | 870 | 4 |
ROW_NUMBER, RANK and DENSE_RANK side by side
WITH orders AS (
SELECT 1001 AS order_id, 250 AS amount UNION ALL
SELECT 1002, 250 UNION ALL
SELECT 1003, 120 UNION ALL
SELECT 1004, 90
)
SELECT
order_id,
amount,
ROW_NUMBER() OVER (ORDER BY amount DESC, order_id) AS row_number,
RANK() OVER (ORDER BY amount DESC) AS rank,
DENSE_RANK() OVER (ORDER BY amount DESC) AS dense_rank
FROM orders
ORDER BY amount DESC, order_id;| order_id | amount | row_number | rank | dense_rank |
|---|---|---|---|---|
| 1001 | 250 | 1 | 1 | 1 |
| 1002 | 250 | 2 | 1 | 1 |
| 1003 | 120 | 3 | 3 | 2 |
| 1004 | 90 | 4 | 4 | 3 |
Top two per category, keeping ties
Two coffee products tie for second place, so the coffee category returns three rows.
WITH product_sales AS (
SELECT 'coffee' AS category, 'beans-1kg' AS product, 540 AS revenue UNION ALL
SELECT 'coffee', 'beans-250g', 310 UNION ALL
SELECT 'coffee', 'drip-bags', 310 UNION ALL
SELECT 'coffee', 'cold-brew', 120 UNION ALL
SELECT 'gear', 'grinder', 890 UNION ALL
SELECT 'gear', 'kettle', 460 UNION ALL
SELECT 'gear', 'scale', 150
)
SELECT
category,
product,
revenue,
RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS revenue_rank
FROM product_sales
QUALIFY revenue_rank <= 2
ORDER BY category, revenue_rank, product;| category | product | revenue | revenue_rank |
|---|---|---|---|
| coffee | beans-1kg | 540 | 1 |
| coffee | beans-250g | 310 | 2 |
| coffee | drip-bags | 310 | 2 |
| gear | grinder | 890 | 1 |
| gear | kettle | 460 | 2 |
NULLs rank first in ascending order
WITH job_runtimes AS (
SELECT 'job-a' AS job, 42 AS runtime_s UNION ALL
SELECT 'job-b', NULL UNION ALL
SELECT 'job-c', 17
)
SELECT
job,
runtime_s,
RANK() OVER (ORDER BY runtime_s) AS rank_default,
RANK() OVER (ORDER BY runtime_s NULLS LAST) AS rank_nulls_last
FROM job_runtimes
ORDER BY job;| job | runtime_s | rank_default | rank_nulls_last |
|---|---|---|---|
| job-a | 42 | 3 | 2 |
| job-b | NULL | 1 | 3 |
| job-c | 17 | 2 | 1 |
Common mistakes
- Expecting exactly N rows from
RANK() <= N. Ties can return more. Use ROW_NUMBER with a tie-breaker for a fixed number of rows. - Expecting consecutive ranks. After a tie RANK skips numbers; use DENSE_RANK for 1, 1, 2 without gaps.
- Omitting ORDER BY.
RANK() OVER ()fails withWindow ORDER BY is required for analytic function rank. - Adding a frame.
RANK() OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)fails withWindow framing clause is not allowed for analytic function rank. - Ranking NULLs first by accident. In ascending order NULL is the smallest value and gets rank 1; add NULLS LAST, or filter NULLs out.
Cost and performance
- Without PARTITION BY the whole input is ranked on a single worker. Google's performance guide suggests reducing the rows before the window, for example ranking only the output of an
ORDER BY ... LIMITsubquery.
FAQ
What is the difference between RANK and DENSE_RANK?
Both give tied rows the same rank. After a tie, RANK skips ahead by the number of tied rows (1, 1, 3) and DENSE_RANK continues with the next integer (1, 1, 2).
How do I get the top N rows per group including ties?
Use QUALIFY RANK() OVER (PARTITION BY group_col ORDER BY metric DESC) <= N. See QUALIFY.
How do I rank in descending order?
Put DESC inside the window: RANK() OVER (ORDER BY revenue DESC). The ORDER BY at the end of the query only sorts the output rows.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
