QUALIFY
constructsupportedQueries & DMLIn a SELECT statement, the QUALIFY clause filters the results of window functions. QUALIFY does with window functions what HAVING does with aggregate functions and GROUP BY clauses. In the execution order of a query, QUALIFY is therefore evaluated after window functions are computed. Typically, a SELECT statement’s clauses are evaluated in the order shown below: - FROM - WHERE - GROUP BY - HAVING - WINDOW - QUALIFY - DISTINCT - ORDER BY - LIMIT
Syntax
QUALIFY <predicate>
The general form of a statement with QUALIFY is similar to the following (some variations in order are allowed, but are not shown)
SELECT <column_list>
FROM <data_source>
[GROUP BY ...]
[HAVING ...]
QUALIFY <predicate>
[ ... ]
Parameters
| Name | Required | Description |
|---|---|---|
column_list | This generally follows the rules for the projection clause of a SELECT statement. | |
data_source | The data source is usually a table, but can be another table-like data source, such as a view, UDTF (user-defined table function), etc. | |
predicate | The predicate is an expression that filters the result after aggregates and window functions are computed. The predicate should look similar to a HAVING clause, but without the keyword HAVING. In addition, the predicate can also contain window functions. See the Examples section (in this topic) for predicate examples. |
Usage notes
- The QUALIFY clause requires at least one window function to be specified in at least one of the following clauses of the SELECT statement: The SELECT column list.
- The filter predicate of the QUALIFY clause.
- Examples of each of these are shown in the Examples section below.
- Expressions in the SELECT list, including window functions, can be referred to by the column alias defined in the SELECT list.
- QUALIFY supports aggregates and subqueries in the predicate. For aggregates, the same rules as for the HAVING clause apply.
- The word QUALIFY is a reserved word.
- The Snowflake syntax for QUALIFY is not part of the ANSI standard.
Parameters
column_list: This generally follows the rules for the projection clause of a SELECT statement. data_source: The data source is usually a table, but can be another table-like data source, such as a view, UDTF (user-defined table function), etc. predicate: The predicate is an expression that filters the result after aggregates and window functions are computed. The predicate should look similar to a HAVING clause, but without the keyword HAVING. In addition, the predicate can also contain window functions. See the Examples section (in this topic) for predicate examples.