QUALIFY Clause
Description
The QUALIFY clause filters rows after window functions have been evaluated.
It can refer to window functions in the SELECT list by alias, or define window
functions directly in the QUALIFY condition. When an alias in the SELECT list
has the same name as an input column, the input column takes precedence.
Syntax
QUALIFY boolean_expression
Parameters
-
boolean_expression
Specifies any expression that evaluates to a result type
boolean. Two or more expressions may be combined together using the logical operators (AND,OR).Note
The current query’s
SELECTlist or theQUALIFYcondition must contain at least one window function. Aggregate functions are not allowed in theQUALIFYcondition.
Examples
CREATE TABLE dealer (id INT, city STRING, car_model STRING, quantity INT);
INSERT INTO dealer VALUES
(100, 'Fremont', 'Honda Civic', 10),
(100, 'Fremont', 'Honda Accord', 15),
(100, 'Fremont', 'Honda CRV', 7),
(200, 'Dublin', 'Honda Civic', 20),
(200, 'Dublin', 'Honda Accord', 10),
(200, 'Dublin', 'Honda CRV', 3),
(300, 'San Jose', 'Honda Civic', 5),
(300, 'San Jose', 'Honda Accord', 8);
-- `QUALIFY` clause referring to a window function in the `SELECT` list by alias.
SELECT city, car_model, RANK() OVER (PARTITION BY car_model ORDER BY quantity) AS rank
FROM dealer
QUALIFY rank = 1;
+--------+------------+----+
| city| car_model|rank|
+--------+------------+----+
|San Jose|Honda Accord| 1|
| Dublin| Honda CRV| 1|
|San Jose| Honda Civic| 1|
+--------+------------+----+
-- `QUALIFY` clause with a window function directly in the predicate.
SELECT city, car_model
FROM dealer
QUALIFY RANK() OVER (PARTITION BY car_model ORDER BY quantity) = 1;
+--------+------------+
| city| car_model|
+--------+------------+
|San Jose|Honda Accord|
| Dublin| Honda CRV|
|San Jose| Honda Civic|
+--------+------------+