QUALIFY clause is a powerful feature of Snowflake – SQL, which allows you to filter the results of a query based on window functions. Window functions are special functions that operate on a set of rows defined by a window or a partition. These functions can be used to calculate aggregate values, ranking, or row numbering to filter your data within the window.
One of the most common window functions used in Snowflake – SQL is ROW_NUMBER(). The ROW_NUMBER() function assigns a unique sequential number to each row within a partition. The partition is defined by one or more columns, and the numbering starts from 1 for each partition. You can then use this numbering to perform various calculations or filtering of the data.
The ROW_NUMBER() function takes no arguments, but it needs to be used with the OVER clause, which defines the window or partition that the function operates on. The OVER clause has two parts, the PARTITION BY clause and the ORDER BY clause.
The PARTITION BY clause specifies the columns that define the partition or grouping of the rows. All the rows with the same values in the partitioning columns are treated as a single group, and the numbering starts from 1 for each group.
The ORDER BY clause specifies the columns used to order the rows within each partition. The order of the rows determines the order in which they are numbered by the ROW_NUMBER() function.
Here is the syntax of the ROW_NUMBER() function with the OVER clause:
ROW_NUMBER() OVER (PARTITION BY column1 [, column2, ...] ORDER BY column3 [, column4, ...])
Now, let’s take an example to see how the QUALIFY ROW_NUMBER() clause works.
Suppose we have a table employees with the following columns:
employee_id
first_name
last_name
department_id
salary
We want to find the top 3 highest-paid employees in each department. We can use the ROW_NUMBER() function to assign a unique number to each employee within their department, ordered by their salary in descending order. Then, we can use the QUALIFY clause to filter the results to only show the first three employees in each department.
Here is the query:
SELECT
employee_id,
first_name,
last_name,
department_id,
salary
FROM
employees
QUALIFY ROW_NUMBER() OVER ( PARTITION BY department_id ORDER BY salary DESC ) <= 3;
In this query, the ROW_NUMBER() function assigns a unique number to each employee within their department, ordered by their salary in descending order. The PARTITION BY clause groups the employees by their department, and the ORDER BY clause orders the employees by their salary.
The QUALIFY clause filters the results to only show the first three employees in each department. This is achieved by specifying the condition ROW_NUMBER() <= 3, which means that only the first three rows for each partition will be returned.
You can also retrieve same data using The ROW_NUMBER() function and the SELECT statement from a result set.
ROW_NUMBER() is a ranking function in Snowflake – SQL that assigns a unique number to each row within a result set, based on a specified order. It can be used to retrieve a specific row by filtering on the row number.
For example, to retrieve the first row of a result set, you can use:
SELECT *
FROM (
SELECT *
, ROW_NUMBER() OVER ( PARTITION BY department_id ORDER BY salary DESC ) AS row_num
FROM employees
) AS tbl
WHERE tbl.row_num = 1;
In here, the ROW_NUMBER() function assigns a unique number to each row based on the column_name specified in the ORDER BY clause. The result set is then filtered using the WHERE clause to return only the row with row_num = 1.
Conclusion
The QUALIFY ROW_NUMBER() clause is a powerful feature of Snowflake – SQL that allows you to filter the results of a query based on window functions. It is particularly useful for ranking and filtering data within partitions or groups, such as finding the top N items in each category.
If you are unsure how to filter your data in Snowflake or would like to speak with an expert to learn more, Anyon Consulting – BI group can help! Our database experts and consultants that can answer any questions on customize dashboards, help with your database implementation, optimize your database platform, and much more. Contact us today to learn more about our Custom Database Development.