SQL QUALIFY clause
A SELECT-level clause evaluated after windows compute, letting you write WHERE rn = 1 logic without a CTE or subquery.
- Kategorie
- Fenster
- Returns
filtered rows- In this category
- 65 of 79
Syntax
SELECT ... FROM t QUALIFY ROW_NUMBER() OVER (PARTITION BY g ORDER BY d) = 1
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Not built in | no QUALIFY, including SQL Server 2025; wrap the window in a CTE or subquery |
| PostgreSQL | Not built in | no QUALIFY through PostgreSQL 18; CTE or subquery required |
| MySQL | Not built in | no QUALIFY; CTE or subquery required |
| SQLite | Not built in | no QUALIFY; CTE or subquery required |
| Oracle | Ja | Oracle AI Database 26ai (release update 23.26.0) and later; earlier 23ai and 19c need the CTE |
Beispiele
Oracle
-- Oracle 26ai, and Snowflake/Teradata/DuckDB/BigQuery SELECT * FROM orders QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY amount DESC) = 1;
PostgreSQL
-- what the other engines require: WITH r AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY amount DESC) AS rn FROM orders ) SELECT * FROM r WHERE rn = 1;
Gut zu wissen
- Of the five engines here only Oracle has QUALIFY, and only from release update 23.26.0 (branded 26ai). It also exists in Teradata, Snowflake, DuckDB, Databricks SQL and BigQuery, so it shows up in code being ported in.
- Evaluation order where it exists: WHERE, then GROUP BY/HAVING, then windows, then QUALIFY, then ORDER BY; it is literally WHERE for window results.
- The portable replacement is mechanical: compute the same expression as a column in a CTE, then filter on that column outside.
Questions about QUALIFY
Does PostgreSQL or SQL Server support QUALIFY?
No, not PostgreSQL 18 and not SQL Server 2025. Both need the CTE or subquery pattern. Oracle added it in release update 23.26.0; Teradata, Snowflake, DuckDB, Databricks and BigQuery have it too.
Is QUALIFY part of the SQL standard?
No. It started in Teradata and spread vendor by vendor, which is why support is patchy. The CTE rewrite gives the same result on any engine with window functions.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.