QUALIFY
Feature Introduction
The QUALIFY clause is used to filter the results of window functions in a SELECT statement. QUALIFY acts on window functions just like HAVING acts on aggregate functions and GROUP BY clauses. In the query execution order, QUALIFY is evaluated after window functions are calculated.
QUALIFY simplifies queries that need to filter window function results, eliminating the need for nested subqueries. This greatly improves the readability and conciseness of SQL statements.
QUALIFY Execution Order
The typical execution order of clauses in a SELECT statement is as follows:
- FROM
- WHERE
- GROUP BY
- HAVING
- Window Functions
- QUALIFY ⭐
- DISTINCT
- ORDER BY
- LIMIT
Syntax
Parameter Description
- column_list: List of columns specified in the SELECT clause
- window_function: Window function expression that must be given an alias for reference in QUALIFY
- alias: Alias of the window function, used in the QUALIFY predicate
- data_source: Data source, typically a table
- predicate: Predicate expression used for filtering, using the alias of the window function
Usage Instructions
Prerequisites
The QUALIFY clause requires that a window function be specified in the SELECT statement and given an alias
Key Characteristics
- When defining a window function in the SELECT clause, an alias must be provided
- Use the alias in the QUALIFY clause for condition evaluation
- Supports all standard window functions
- Supports various comparison operators (=, !=, <, >, <=, >=, IN, etc.)
- QUALIFY is a reserved word
Usage Examples
Example 1: Get the First Record of Each Group
Scenario Description: Use ROW_NUMBER to get records with row number 1 in each group
Test Table:
SQL Statement:
Result:
Example 2: Get Records with Row Number Greater Than 1
SQL Statement:
Result:
Example 3: Use RANK Function to Get Top 2 Rankings
SQL Statement:
Result:
Example 4: Use DENSE_RANK Function
SQL Statement:
Result:
Example 5: Use Aggregate Window Functions
Scenario Description: Get all rows where the group sum is greater than or equal to 3
SQL Statement:
Result:
Comparison with Traditional Subqueries
Traditional Method (Using Subqueries)
QUALIFY Method (Recommended)
Comparison of Advantages
| Aspect | Traditional Method | QUALIFY Method |
|---|---|---|
| Code Complexity | High (Nested Subqueries) | Low (Flat) |
| Readability | Average | Excellent ✓ |
| Lines of Code | 4-5 Lines | 3 Lines |
| Performance | Equivalent | Equivalent |
| Maintenance Cost | High | Low ✓ |
Important Notes
Rules That Must Be Followed
- Window functions must be defined in the SELECT clause and given an alias
- Use the alias of the window function in QUALIFY for condition evaluation
- QUALIFY must reference at least one window function
