Request a demo

BigQuery · Query syntax

BigQuery QUALIFY

QUALIFY filters rows on the result of a window function, the way HAVING filters on aggregates. Use it to keep the latest row per key or the top N per group without a subquery: QUALIFY ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) = 1.

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

Window functions are computed after WHERE, GROUP BY and HAVING, so you cannot filter on them in WHERE; that fails with Analytic function not allowed in WHERE clause. QUALIFY is the clause that runs after window functions: BigQuery evaluates FROM, WHERE, GROUP BY and aggregation, HAVING, WINDOW, then QUALIFY, then DISTINCT, ORDER BY and LIMIT. It keeps rows whose condition is TRUE and drops rows where it is FALSE or NULL.

The condition must involve a window function, either written in QUALIFY itself or referenced through an alias from the SELECT list (QUALIFY rn = 1). You do not need to select the window value at all: QUALIFY ROW_NUMBER() OVER (...) = 1 filters without adding a column, which also keeps SELECT * clean. Older BigQuery versions required a WHERE, GROUP BY or HAVING clause next to QUALIFY, which is why you still see WHERE TRUE in many examples; current BigQuery runs QUALIFY on its own.

The typical uses are deduplication (keep one row per key), latest state per entity (the newest status of each order), and top-N per group. The choice of function decides ties: ROW_NUMBER() keeps exactly one row per partition and breaks ties arbitrarily unless the ORDER BY is unique, RANK() keeps every row tied for first place, and DENSE_RANK() keeps ties without gaps. Snowflake, Databricks and Teradata use the same clause; PostgreSQL, MySQL and Redshift do not have it, and there you wrap the window function in a subquery.

Syntax

SELECT ...
FROM ...
[WHERE ...]
[GROUP BY ...]
[HAVING ...]
QUALIFY bool_expression
[ORDER BY ...]
[LIMIT ...]

Arguments and return value

ArgumentDescription
bool_expressionA condition that contains a window function, or an alias of a window function from the SELECT list. Rows where it is TRUE are kept.

Returns: The rows of the query for which the QUALIFY condition is TRUE. It does not change columns.

Examples

Keep the latest version of each record

Order 1001 has two status updates; only the newest survives. The window value is not selected.

WITH order_updates AS (
  SELECT 1001 AS order_id, 'paid' AS status, TIMESTAMP '2026-11-20 09:00:00' AS updated_at UNION ALL
  SELECT 1001, 'shipped', TIMESTAMP '2026-11-21 14:30:00' UNION ALL
  SELECT 1002, 'paid', TIMESTAMP '2026-11-21 08:15:00'
)
SELECT order_id, status, updated_at
FROM order_updates
QUALIFY ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY updated_at DESC) = 1
ORDER BY order_id;
Result
order_idstatusupdated_at
1001shipped2026-11-21 14:30:00 UTC
1002paid2026-11-21 08:15:00 UTC

Top 2 products per category, filtering on a SELECT alias

WITH sales AS (
  SELECT 'audio' AS category, 'headphones' AS product, 320 AS units UNION ALL
  SELECT 'audio', 'speaker', 210 UNION ALL
  SELECT 'audio', 'microphone', 45 UNION ALL
  SELECT 'video', 'webcam', 150 UNION ALL
  SELECT 'video', 'capture card', 90
)
SELECT
  category,
  product,
  units,
  RANK() OVER (PARTITION BY category ORDER BY units DESC) AS sales_rank
FROM sales
QUALIFY sales_rank <= 2
ORDER BY category, sales_rank;
Result
categoryproductunitssales_rank
audioheadphones3201
audiospeaker2102
videowebcam1501
videocapture card902

Ties: ROW_NUMBER keeps one row, RANK keeps all tied rows

Two invoices share the highest amount. With RANK() = 1 both are kept; ROW_NUMBER() = 1 would keep one of them, chosen arbitrarily.

WITH invoices AS (
  SELECT 'acme' AS customer, 'INV-1' AS invoice_id, 500 AS amount UNION ALL
  SELECT 'acme', 'INV-2', 500 UNION ALL
  SELECT 'acme', 'INV-3', 120
)
SELECT customer, invoice_id, amount
FROM invoices
QUALIFY RANK() OVER (PARTITION BY customer ORDER BY amount DESC) = 1
ORDER BY invoice_id;
Result
customerinvoice_idamount
acmeINV-1500
acmeINV-2500

Deduplicate a staging table before a MERGE

A real-world pattern: keep the newest row per key so that the MERGE does not fail on duplicate source rows.

SELECT *
FROM `project.dataset.customers_staging`
WHERE load_date = '2026-11-22'
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1;

Common mistakes

  • Filtering a window function in WHERE. WHERE ROW_NUMBER() OVER (...) = 1 fails because WHERE runs before window functions; move the condition to QUALIFY.
  • Deduplicating with ROW_NUMBER() over a non-unique ORDER BY. When two rows tie on updated_at, which one survives can change between runs. Add a tiebreaker column, such as ORDER BY updated_at DESC, ingestion_id DESC.
  • Using QUALIFY when you meant HAVING. A condition on an aggregate such as COUNT(*) > 1 belongs in HAVING; QUALIFY needs a window function.
  • Porting QUALIFY to PostgreSQL, MySQL or Redshift. They have no QUALIFY; wrap the query and filter the window column in an outer WHERE.

Cost and performance

  • QUALIFY does not reduce bytes scanned: the window function needs all rows that pass WHERE. Put partition and other filters in WHERE so fewer rows reach the window.
  • Each distinct OVER (PARTITION BY ...) needs its rows grouped by key, which is a shuffle. A very large single partition (for example a NULL key that most rows share) can exceed memory and fail with Resources exceeded; filter out NULL keys first.

FAQ

How do I get the latest row per group in BigQuery?

QUALIFY ROW_NUMBER() OVER (PARTITION BY group_key ORDER BY updated_at DESC) = 1. An alternative without window functions is ARRAY_AGG(t ORDER BY updated_at DESC LIMIT 1)[OFFSET(0)] grouped by the key.

What is the difference between QUALIFY, HAVING and WHERE?

WHERE filters rows before grouping, HAVING filters groups after aggregation, and QUALIFY filters rows after window functions are computed.

Does QUALIFY need a WHERE clause in BigQuery?

No. Early versions required WHERE, GROUP BY or HAVING alongside QUALIFY, hence the WHERE TRUE in older examples, but current BigQuery accepts QUALIFY on its own.

Errors you may hit

More query syntax

All BigQuery query syntax · All statements and clauses · 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.