BigQuery ROW_NUMBER, RANK, DENSE_RANK and NTILE, with tested examples
BigQuery's numbering functions give each row a position within its group: ROW_NUMBER, RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST and NTILE. This guide runs all six on the same four rows, including a tie, so the differences are visible side by side, then covers where NULLs sort, how to keep one row per group, and APPROX_QUANTILES for percentiles.
The six numbering functions all answer “what position is this row in its group”, and they differ only in how they treat ties. That is why the choice matters: on data without ties they return the same numbers, and the difference appears the first time two rows are equal.
| Function | With a tie for second place | Use it for |
|---|---|---|
ROW_NUMBER() |
1, 2, 3, 4: the tie is broken arbitrarily | one row per group, deduplication |
RANK() |
1, 2, 2, 4: a gap after the tie | competition ranking |
DENSE_RANK() |
1, 2, 2, 3: no gap | “the top 3 distinct values” |
PERCENT_RANK() |
0 to 1, ties share a value | relative position |
CUME_DIST() |
share of rows at or above this one | “top 25%” |
NTILE(n) |
n buckets of equal size | quartiles, deciles |
All six are window functions, so they need OVER (... ORDER BY ...). The frame and partition rules are in the window functions guide.
Note: Originally published in January 2024. Fully rewritten in October 2026: every result below was produced by running the query in BigQuery on 16 October 2026.
Reading to get this done?
All six on one table
WITH sales AS (
SELECT DATE '2026-09-01' AS day, 60 AS revenue
UNION ALL SELECT DATE '2026-09-02', 60
UNION ALL SELECT DATE '2026-09-03', 90
UNION ALL SELECT DATE '2026-09-04', 30
)
SELECT
day, revenue,
ROW_NUMBER() OVER w AS row_number,
RANK() OVER w AS rank,
DENSE_RANK() OVER w AS dense_rank,
ROUND(PERCENT_RANK() OVER w, 3) AS percent_rank,
ROUND(CUME_DIST() OVER w, 3) AS cume_dist,
NTILE(2) OVER w AS ntile_2
FROM sales
WINDOW w AS (ORDER BY revenue DESC);| day | revenue | row_number | rank | dense_rank | percent_rank | cume_dist | ntile_2 |
|---|---|---|---|---|---|---|---|
| 2026-09-03 | 90 | 1 | 1 | 1 | 0 | 0.25 | 1 |
| 2026-09-01 | 60 | 2 | 2 | 2 | 0.333 | 0.75 | 1 |
| 2026-09-02 | 60 | 3 | 2 | 2 | 0.333 | 0.75 | 2 |
| 2026-09-04 | 30 | 4 | 4 | 3 | 1 | 1 | 2 |
WINDOW w AS (...) names the window once so six functions can share it.
Read the two 60s across the row:
ROW_NUMBERsplits them, 2 and 3, and which day gets 2 is not defined. Run it again and they may swap.RANKgives both 2 and skips 3. The next row is 4, because three rows are ahead of it.DENSE_RANKgives both 2 and continues with 3.PERCENT_RANKis(rank - 1) / (rows - 1): 0 for the first, 1 for the last.CUME_DISTis the share of rows ranked at or before this one: both 60s are at 0.75, because three of four rows are 60 or more.NTILE(2)puts the two 60s in different buckets. Buckets are filled by row count, not by value, so tied rows can fall on either side of a bucket edge. If that matters, bucket withCUME_DISTinstead:CUME_DIST() <= 0.5.
Make ROW_NUMBER repeatable
A query that keeps ROW_NUMBER() = 1 keeps an arbitrary row whenever there is a tie, and a report built on it can change between runs with no change in the data. Add columns to ORDER BY until the order is unique:
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC, order_id DESC)You need one row per customer in BigQuery, and ROW_NUMBER keeps a different one whenever two rows tie.
Paste this into your AI assistant. It writes the ROW_NUMBER, RANK or DENSE_RANK query for your BigQuery tables, with a tie-break that returns the same row on every run.
The prompt
Read https://www.owox.com/ai-agent-setup.md and follow it to set up OWOX Data Marts for me, starting from /blog/articles/bigquery-numbering-functions.md. The task: write me a BigQuery query that keeps the latest row per customer in my orders table, with a tie-break so the same row wins on every run, and ranks customers by revenue. Tag the links you give me with utm_campaign=ai-task and utm_term=data-foundation. Use what you know about my context and goals
Where NULLs go
SELECT
v,
ROW_NUMBER() OVER (ORDER BY v) AS asc_rn,
ROW_NUMBER() OVER (ORDER BY v DESC) AS desc_rn,
ROW_NUMBER() OVER (ORDER BY v ASC NULLS LAST) AS asc_nulls_last
FROM UNNEST([30, NULL, 90]) AS v;| v | asc_rn | desc_rn | asc_nulls_last |
|---|---|---|---|
| NULL | 1 | 3 | 3 |
| 30 | 2 | 2 | 1 |
| 90 | 3 | 1 | 2 |
BigQuery sorts NULLs first in ascending order and last in descending order. So “the smallest value per group” with ORDER BY v returns a NULL if the group has one. Add NULLS LAST, or filter the NULLs out first.
Keep one row per group
The most common use of ROW_NUMBER is deduplication: the latest record per customer, the first session per user. QUALIFY filters on it without a subquery:
SELECT *
FROM orders
QUALIFY ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC, order_id DESC) = 1;On a very large table, a single partition with millions of rows can fail with “Resources exceeded”. Two ways out: partition by something finer, or replace the window with an aggregate, ARRAY_AGG(t ORDER BY order_date DESC LIMIT 1)[OFFSET(0)] grouped by customer, which does the same job with less memory. The aggregate functions guide covers ARRAY_AGG ... LIMIT and MAX_BY.
Top N per group
“Top 3 products per category” depends on what you want from ties:
- exactly three rows:
ROW_NUMBER() <= 3, with a tiebreaker; - everything tied with the third:
RANK() <= 3; - the three highest distinct values and every row that has them:
DENSE_RANK() <= 3.
The difference between two rows, such as today against yesterday, is not a numbering function but LAG; see the navigation functions guide.
Percentiles: APPROX_QUANTILES
For a median or quartiles over a whole column, APPROX_QUANTILES is simpler than ranking every row. It is an aggregate, not a window function:
SELECT
APPROX_QUANTILES(x, 4) AS quartiles,
APPROX_QUANTILES(x, 100)[OFFSET(50)] AS median
FROM UNNEST([30, 60, 60, 90, 100, 120, 140]) AS x;| quartiles | median |
|---|---|
| [30, 60, 90, 120, 140] | 90 |
APPROX_QUANTILES(x, 4) returns five values: the minimum, three quartile boundaries and the maximum. As the name says, results on large data are approximate. For other statistics, see statistical aggregate functions.
The ranking is a definition
“Top customer” depends on the tie rule, the NULL order and the tiebreaker, and two analysts who choose differently publish two different top-10 lists. Decide once, write it next to the column, and publish the query as a Data Mart in OWOX Data Marts, so every sheet and ranks the same way. OWOX Data Marts runs the SQL as written, in your own BigQuery project, and does not transform the data. Other function families are mapped in the BigQuery functions overview.
You need one row per customer in BigQuery, and ROW_NUMBER keeps a different one whenever two rows tie.
Paste this into your AI assistant. It writes the ROW_NUMBER, RANK or DENSE_RANK query for your BigQuery tables, with a tie-break that returns the same row on every run.
The prompt
Read https://www.owox.com/ai-agent-setup.md and follow it to set up OWOX Data Marts for me, starting from /blog/articles/bigquery-numbering-functions.md. The task: write me a BigQuery query that keeps the latest row per customer in my orders table, with a tie-break so the same row wins on every run, and ranks customers by revenue. Tag the links you give me with utm_campaign=ai-task and utm_term=data-foundation. Use what you know about my context and goals
Prefer to start on your own? Get started free
Frequently Asked Questions
How do I keep one row per group with ROW_NUMBER in BigQuery?
Number the rows within each group and keep the first: QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC, order_id DESC) = 1. Add columns to ORDER BY until the order is unique, or a tie returns a different row on each run.
How do I fix 'Resources exceeded' with ROW_NUMBER on a large table?
A single partition is processed in one place, so a partition with millions of rows can run out of memory. Partition by something finer, filter first, or replace the window with an aggregate such as ARRAY_AGG(t ORDER BY order_date DESC LIMIT 1)[OFFSET(0)] grouped by the key.
What does CUME_DIST return in BigQuery?
The share of rows ranked at or before the current one, from 0 to 1. With revenue of 90, 60, 60 and 30 in descending order, both 60s get 0.75. CUME_DIST() <= 0.25 is a simple way to take the top quarter of rows, and tied rows always land in the same bucket.
What is the difference between ROW_NUMBER, RANK and DENSE_RANK in BigQuery?
They differ only on ties. For 90, 60, 60, 30: ROW_NUMBER returns 1, 2, 3, 4 and breaks the tie arbitrarily; RANK returns 1, 2, 2, 4 with a gap; DENSE_RANK returns 1, 2, 2, 3 without one.
How does NTILE work in BigQuery?
NTILE(n) splits the ordered rows into n buckets of nearly equal size and returns the bucket number. Buckets are filled by row count, not by value, so two tied rows can land in different buckets.
What does PERCENT_RANK return in BigQuery?
The relative position of a row: (rank - 1) / (rows - 1), from 0 for the first row to 1 for the last. Tied rows share a value. It is useful for showing where an item stands within its group, for example a product's position among its category's sellers.

