BigQuery · Window function
BigQuery ROW_NUMBER
ROW_NUMBER() numbers the rows of each window partition 1, 2, 3 in ORDER BY order, with no ties and no gaps. Its main use is removing duplicates: QUALIFY ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) = 1 keeps the latest row per key.
ROW_NUMBER gives every row of a partition a unique sequential number, starting at 1 in each partition. Rows that tie in the ORDER BY still get different numbers, and which of the tied rows gets the lower number is not defined, so it can change between runs. Add columns to ORDER BY until each row's position is unique if the result has to be reproducible. ORDER BY itself is optional, but without it the numbering is arbitrary.
The standard deduplication query keeps row number 1 per key. QUALIFY filters on a window function directly, so no subquery is needed: SELECT * FROM customers QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1. A window function can't appear in WHERE, because it is evaluated after WHERE, GROUP BY and HAVING; BigQuery rejects it with Analytic function not allowed in WHERE clause.
ROW_NUMBER takes no arguments and no window frame. Without PARTITION BY all rows form one partition, and Google's performance guide notes that such a window is sorted on a single worker, so numbering a large table, for example to create surrogate keys, can fail with Resources exceeded. For unique keys use GENERATE_UUID or a hash such as FARM_FINGERPRINT instead.
Syntax
ROW_NUMBER() OVER ([PARTITION BY partition_expression [, ...]] [ORDER BY expression [{ASC | DESC}] [, ...]])Arguments and return value
| Argument | Description |
|---|---|
PARTITION BY partition_expression | Restarts the numbering for each distinct value or combination of values. FLOAT64 expressions are not allowed. |
ORDER BY expression | Sets the order of numbering. Optional, but without it the numbers are arbitrary. NULLs sort first in ASC and last in DESC unless you add NULLS FIRST or NULLS LAST. |
Returns: INT64, starting at 1 in every partition.
Examples
Number each customer's orders
WITH orders AS (
SELECT 'c1' AS customer_id, 1001 AS order_id, DATE '2026-11-02' AS order_date UNION ALL
SELECT 'c1', 1004, DATE '2026-11-15' UNION ALL
SELECT 'c2', 1002, DATE '2026-11-05' UNION ALL
SELECT 'c1', 1003, DATE '2026-11-09'
)
SELECT
customer_id,
order_id,
order_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS order_seq
FROM orders
ORDER BY customer_id, order_seq;| customer_id | order_id | order_date | order_seq |
|---|---|---|---|
| c1 | 1001 | 2026-11-02 | 1 |
| c1 | 1003 | 2026-11-09 | 2 |
| c1 | 1004 | 2026-11-15 | 3 |
| c2 | 1002 | 2026-11-05 | 1 |
Remove duplicates: keep the latest row per key
WITH customer_updates AS (
SELECT 'c1' AS customer_id, 'ana@old.example' AS email, TIMESTAMP '2026-10-01 08:00:00 UTC' AS updated_at UNION ALL
SELECT 'c1', 'ana@new.example', TIMESTAMP '2026-11-12 10:30:00 UTC' UNION ALL
SELECT 'c2', 'li@example.org', TIMESTAMP '2026-11-03 17:45:00 UTC'
)
SELECT *
FROM customer_updates
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1
ORDER BY customer_id;| customer_id | updated_at | |
|---|---|---|
| c1 | ana@new.example | 2026-11-12 10:30:00 UTC |
| c2 | li@example.org | 2026-11-03 17:45:00 UTC |
Break ties to make the numbering repeatable
e-17 and e-18 share a timestamp. Ordered by event_ts alone they could be numbered either way; event_id as a second key fixes the order.
WITH events AS (
SELECT 'u1' AS user_id, 'e-17' AS event_id, TIMESTAMP '2026-11-20 10:00:00 UTC' AS event_ts UNION ALL
SELECT 'u1', 'e-18', TIMESTAMP '2026-11-20 10:00:00 UTC' UNION ALL
SELECT 'u1', 'e-19', TIMESTAMP '2026-11-20 10:05:00 UTC'
)
SELECT
event_id,
event_ts,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_ts, event_id) AS seq
FROM events
ORDER BY seq;| event_id | event_ts | seq |
|---|---|---|
| e-17 | 2026-11-20 10:00:00 UTC | 1 |
| e-18 | 2026-11-20 10:00:00 UTC | 2 |
| e-19 | 2026-11-20 10:05:00 UTC | 3 |
Top two products per category
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', '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
FROM product_sales
QUALIFY ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC) <= 2
ORDER BY category, revenue DESC;| category | product | revenue |
|---|---|---|
| coffee | beans-1kg | 540 |
| coffee | beans-250g | 310 |
| gear | grinder | 890 |
| gear | kettle | 460 |
Common mistakes
- Leaving ties unresolved. If two rows share the ORDER BY value, ROW_NUMBER orders them arbitrarily, and a deduplication query can keep a different row on each run. Make the last ORDER BY key unique, such as an event ID or an ingestion timestamp.
- Filtering in WHERE.
WHERE ROW_NUMBER() OVER (...) = 1fails with Analytic function not allowed in WHERE clause. Use QUALIFY or filter in an outer query. - Using RANK for deduplication. RANK gives tied rows the same number, so
= 1can keep several rows per key. ROW_NUMBER always keeps exactly one. - Numbering a whole large table.
ROW_NUMBER() OVER (ORDER BY created_at)without PARTITION BY runs on one worker and can run out of memory. Partition the window, or use GENERATE_UUID when you only need unique IDs. - Expecting
ROW_NUMBER() OVER ()to follow load order. Tables have no inherent row order; without ORDER BY the numbers can change between runs.
Cost and performance
- A window without PARTITION BY is sorted on a single worker. Partition by the deduplication key, and filter or pre-aggregate rows before the window runs.
- QUALIFY filters after the table is read, so it doesn't reduce bytes billed. Put partition filters in WHERE, for example
WHERE event_date >= '2026-11-01' QUALIFY ROW_NUMBER() OVER (...) = 1, so BigQuery scans only those partitions.
FAQ
How do I remove duplicates in BigQuery?
Keep one row per key with QUALIFY ROW_NUMBER() OVER (PARTITION BY key ORDER BY updated_at DESC) = 1. For rows that are identical in every column, SELECT DISTINCT * is enough unless the table has JSON or GEOGRAPHY columns, which can't be compared.
What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
They differ on ties. For two rows tied in first place followed by one more row, ROW_NUMBER gives 1, 2, 3, RANK gives 1, 1, 3 and DENSE_RANK gives 1, 1, 2.
Can I use ROW_NUMBER in a WHERE clause?
No. Use QUALIFY, which runs after window functions are computed, or compute the row number in a subquery and filter it in the outer WHERE.
How do I add an auto-increment ID in BigQuery?
BigQuery has no auto-increment column. ROW_NUMBER() OVER (ORDER BY ...) works for small or partitioned data; for unique keys at scale use GENERATE_UUID() or a hash of the natural key with FARM_FINGERPRINT.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
