Request a demo

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.

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

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

ArgumentDescription
PARTITION BY partition_expressionRanks each group separately. Optional.
ORDER BY expressionRequired. 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;
Result
playerpointsrank
ben10201
ana9802
chen9802
dara8704

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;
Result
order_idamountrow_numberrankdense_rank
1001250111
1002250211
1003120332
100490443

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;
Result
categoryproductrevenuerevenue_rank
coffeebeans-1kg5401
coffeebeans-250g3102
coffeedrip-bags3102
geargrinder8901
gearkettle4602

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;
Result
jobruntime_srank_defaultrank_nulls_last
job-a4232
job-bNULL13
job-c1721

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 with Window ORDER BY is required for analytic function rank.
  • Adding a frame. RANK() OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) fails with Window 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 ... LIMIT subquery.

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.

Errors you may hit

More window functions

All BigQuery window 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.