- The
QUALIFYclause filters the results of window functions. - This filtering of results is similar to how a
HAVINGclause filters the results of aggregate functions applied based on theGROUP BYclause. - A window function is required to be present either in the
QUALIFYclause or theSELECTlist. - The QUALIFY clause avoids the need for a subquery or WITH clause to perform this filtering (much like HAVING avoids a subquery).
QUALIFY clause is typically completed in this order:
FROMWHEREGROUP BYand aggregationHAVING(filters the results of aggregation)WINDOWQUALIFY(filters the results of window functions)DISTINCTORDER BYLIMIT
Example
Lets create a tableemployee with the following data:
Find Employees with Lowest Salary for Each Job Title
Using CTE:
Using Sub-query:
Using
QUALIFY clause:
Different ways to write the same query using
QUALFIY clause to get the same result.
Window function in SELECT column list
Window function in SELECT column list
Window function in the QUALIFY clause but using WINDOW clause
Window function in the QUALIFY clause but using WINDOW clause
Window function in the SELECT clause but using WINDOW clause
Window function in the SELECT clause but using WINDOW clause