BigQuery error · Query and syntax errors
Analytic function not allowed in WHERE clause
A window (analytic) function such as ROW_NUMBER, RANK or LAG is used in WHERE, which is evaluated before window functions. Filter on it with QUALIFY instead, or compute it in a subquery and filter in the outer query.
The error message
Analytic function not allowed in WHERE clause at [<line>:<column>]
Causes
Deduplication written with ROW_NUMBER in WHERE
WHERE ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_ts DESC) = 1 is the most common trigger. WHERE runs before window functions are computed, so their values don't exist yet.
Window functions in other early clauses
The same rule applies to other clauses that run before window functions; the message then names that clause, for example Analytic function not allowed in GROUP BY or Analytic function not allowed in HAVING clause.
Expecting a SELECT alias to work in WHERE
Moving the window function into the SELECT list and filtering on its alias in WHERE gives Unrecognized name instead, for the same reason: WHERE runs first.
How to fix it
1. Filter with QUALIFY
QUALIFY filters the result of window functions in the same query level. It can reference the window function directly or its SELECT alias.
WITH events AS (
SELECT 'u1' AS user_id, 'view' AS event_name, TIMESTAMP '2026-11-22 10:00:00 UTC' AS event_ts UNION ALL
SELECT 'u1', 'purchase', TIMESTAMP '2026-11-22 10:05:00 UTC' UNION ALL
SELECT 'u2', 'view', TIMESTAMP '2026-11-22 09:00:00 UTC'
)
SELECT user_id, event_name, event_ts
FROM events
WHERE event_ts >= TIMESTAMP '2026-11-22 00:00:00 UTC'
QUALIFY ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_ts DESC) = 1;2. Filter in an outer query
Compute the window function in a subquery or CTE, then filter on it with WHERE in the next step. This works in every SQL dialect.
WITH orders AS (
SELECT 'c1' AS customer_id, 1 AS order_id, 30.0 AS amount UNION ALL
SELECT 'c1', 2, 12.0 UNION ALL
SELECT 'c2', 3, 50.0
),
ranked AS (
SELECT customer_id, order_id, amount,
RANK() OVER (PARTITION BY customer_id ORDER BY amount DESC) AS amount_rank
FROM orders
)
SELECT customer_id, order_id, amount
FROM ranked
WHERE amount_rank = 1;How to prevent it
- Keep ordinary row filters in WHERE and window filters in QUALIFY in the same query: WHERE reduces the rows before the window is computed, which keeps the sort smaller.
- Partition window functions by a key. A ROW_NUMBER over the whole table sorts everything on one worker and can fail with resources exceeded.
FAQ
Does BigQuery support QUALIFY?
Yes. QUALIFY filters on window functions and needs a window function in the QUALIFY clause or the SELECT list. It comes after WHERE, GROUP BY and HAVING in the query.
Is a query that fails with this error billed?
No. It fails during analysis, before data is read, so nothing is billed on-demand and no slot time is used under editions.
Is QUALIFY cheaper than a subquery?
They read the same columns and usually produce the same plan, so the bytes billed are the same. QUALIFY is shorter to write; the cost depends on the columns and partitions read and on the size of the windows.
Related
SQL reference
More query and syntax errors
All BigQuery error messages · What 50M daily events say about BigQuery failures
