Category: Data Query Language (DQL)
Platform Support:
✅ Snowflake | ✅ BigQuery | ❌ Databricks
Description
QUALIFY filters the results of window functions, similar to how HAVING filters aggregate functions. It eliminates the need for a subquery when filtering on window function results. This is particularly useful for ranking, row numbering, and other analytical queries.
Syntax
Platform-Specific Notes
Snowflake:
Full support for QUALIFY
Very efficient, recommended over subqueries
Can reference window functions defined in SELECT or directly in QUALIFY
BigQuery:
Full support introduced in 2020
Works identically to Snowflake
Improves query readability
Databricks:
❌ Not supported
Must use subquery pattern or temporary tables instead
Example 1: Top N per Group (Snowflake/BigQuery)
Databricks Alternative:
Sample Data (employees table):
department | employee_name | salary |
|---|---|---|
Sales | Alice | 75000 |
Sales | Bob | 65000 |
Sales | Carol | 70000 |
IT | Diana | 90000 |
IT | Eric | 85000 |
IT | Frank | 80000 |
Marketing | Grace | 72000 |
Marketing | Henry | 68000 |
Result:
department | employee_name | salary | salary_rank |
|---|---|---|---|
IT | Diana | 90000 | 1 |
IT | Eric | 85000 | 2 |
Marketing | Grace | 72000 | 1 |
Marketing | Henry | 68000 | 2 |
Sales | Alice | 75000 | 1 |
Sales | Carol | 70000 | 2 |
Example 2: Get Most Recent Record per Customer
Snowflake/BigQuery:
Sample Data (orders table):
customer_id | order_date | order_total |
|---|---|---|
101 | 2024-01-15 | 250.00 |
101 | 2024-02-20 | 180.00 |
102 | 2024-01-10 | 99.99 |
102 | 2024-03-05 | 450.00 |
103 | 2024-02-14 | 320.00 |
Result:
customer_id | order_date | order_total | rn |
|---|---|---|---|
101 | 2024-02-20 | 180.00 | 1 |
102 | 2024-03-05 | 450.00 | 1 |
103 | 2024-02-14 | 320.00 | 1 |
Performance Tips
Snowflake/BigQuery: QUALIFY is more efficient than subqueries for window function filtering
Define the window function once in SELECT if you need the ranked column in output
For Databricks, consider using CTEs for better readability of the subquery approach





