SQL QUALIFY: filtering on window functions
QUALIFY is a SQL clause that filters rows on the result of a window function, the way HAVING filters on the result of an aggregate. It turns the common latest-row-per-key query into one SELECT instead of a subquery.
Why WHERE cannot do it
A query runs its clauses in a fixed order: FROM, WHERE, GROUP BY, HAVING, then window functions, then QUALIFY, then ORDER BY and LIMIT. WHERE runs before any window function has a value, so a filter like WHERE row_number() OVER (...) = 1 is an error. DuckDB rejects it with "WHERE clause cannot contain window functions". QUALIFY runs after the windows, so it can see them.
Latest row per key
The most common use is deduplication: keep the newest order per customer, the latest version of each record, or the top N per group.
CREATE TABLE orders (order_id INTEGER, customer_id INTEGER, ordered_at DATE, total DECIMAL(10,2)); INSERT INTO orders VALUES (1, 10, DATE '2026-10-01', 40.00), (2, 10, DATE '2026-10-15', 25.00), (3, 20, DATE '2026-10-03', 90.00), (4, 20, DATE '2026-11-02', 15.00), (5, 30, DATE '2026-11-05', 60.00); SELECT customer_id, order_id, ordered_at, total FROM orders QUALIFY row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) = 1 ORDER BY customer_id;
This returns orders 2, 4 and 5, one per customer. Use rank() instead of row_number() to keep ties, and <= 3 instead of = 1 for the top three. In DuckDB, QUALIFY can also refer to a window column by its alias from the SELECT list.
QUALIFY combines with GROUP BY too. This keeps customers whose spend is above the average customer's spend, which returns customer 20 with 105.00:
SELECT customer_id, sum(total) AS spend FROM orders GROUP BY customer_id QUALIFY sum(total) > avg(sum(total)) OVER ();
Which engines support QUALIFY
| Engine | QUALIFY |
|---|---|
| DuckDB, Snowflake, BigQuery, Databricks SQL, Teradata | Yes |
| PostgreSQL, MySQL, SQL Server | No: use a subquery or CTE |
QUALIFY is not part of the SQL standard. It started in Teradata and spread to the analytical engines. Code that has to run on PostgreSQL, MySQL or SQL Server needs the subquery form.
The portable equivalent
Compute the window in a subquery or CTE, then filter it in the outer WHERE. The result is identical; the cost is one more level of nesting and an extra column (rn) to exclude.
SELECT customer_id, order_id, ordered_at, total
FROM (
SELECT o.*,
row_number() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) AS rn
FROM orders AS o
) AS t
WHERE rn = 1
ORDER BY customer_id;Performance is the same in both forms: the engine still sorts or hashes each partition to compute the window. QUALIFY is a readability feature, not an optimization.
In Thunderduck
Thunderduck runs DuckDB's dialect, so QUALIFY works in any query, including ones that read PostgreSQL, MySQL or SQL Server tables, which do not support it natively. A plain filter with constants on the remote table is still pushed down to the source; the window function and the QUALIFY run in Thunderduck. It is a short way to deduplicate rows before loading them into an Iceberg table with CREATE TABLE AS SELECT.
Docs: https://docs.thunderduck.ai/reference/sql/