|
| 1 | +--- |
| 2 | +layout: global |
| 3 | +title: QUALIFY Clause |
| 4 | +displayTitle: QUALIFY Clause |
| 5 | +license: | |
| 6 | + Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + contributor license agreements. See the NOTICE file distributed with |
| 8 | + this work for additional information regarding copyright ownership. |
| 9 | + The ASF licenses this file to You under the Apache License, Version 2.0 |
| 10 | + (the "License"); you may not use this file except in compliance with |
| 11 | + the License. You may obtain a copy of the License at |
| 12 | +
|
| 13 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +--- |
| 21 | + |
| 22 | +### Description |
| 23 | + |
| 24 | +The `QUALIFY` clause filters rows after window functions have been evaluated. |
| 25 | +It can refer to window functions in the `SELECT` list by alias, or define window |
| 26 | +functions directly in the `QUALIFY` condition. |
| 27 | + |
| 28 | +### Syntax |
| 29 | + |
| 30 | +```sql |
| 31 | +QUALIFY boolean_expression |
| 32 | +``` |
| 33 | + |
| 34 | +### Parameters |
| 35 | + |
| 36 | +* **boolean_expression** |
| 37 | + |
| 38 | + Specifies any expression that evaluates to a result type `boolean`. Two or |
| 39 | + more expressions may be combined together using the logical |
| 40 | + operators ( `AND`, `OR` ). |
| 41 | + |
| 42 | + **Note** |
| 43 | + |
| 44 | + The current query's `SELECT` list or the `QUALIFY` condition must contain at least |
| 45 | + one window function. Aggregate functions are not allowed in the `QUALIFY` condition. |
| 46 | + |
| 47 | +### Examples |
| 48 | + |
| 49 | +```sql |
| 50 | +CREATE TABLE dealer (id INT, city STRING, car_model STRING, quantity INT); |
| 51 | +INSERT INTO dealer VALUES |
| 52 | + (100, 'Fremont', 'Honda Civic', 10), |
| 53 | + (100, 'Fremont', 'Honda Accord', 15), |
| 54 | + (100, 'Fremont', 'Honda CRV', 7), |
| 55 | + (200, 'Dublin', 'Honda Civic', 20), |
| 56 | + (200, 'Dublin', 'Honda Accord', 10), |
| 57 | + (200, 'Dublin', 'Honda CRV', 3), |
| 58 | + (300, 'San Jose', 'Honda Civic', 5), |
| 59 | + (300, 'San Jose', 'Honda Accord', 8); |
| 60 | + |
| 61 | +-- `QUALIFY` clause referring to a window function in the `SELECT` list by alias. |
| 62 | +SELECT city, car_model, RANK() OVER (PARTITION BY car_model ORDER BY quantity) AS rank |
| 63 | +FROM dealer |
| 64 | +QUALIFY rank = 1; |
| 65 | ++--------+------------+----+ |
| 66 | +| city| car_model|rank| |
| 67 | ++--------+------------+----+ |
| 68 | +|San Jose|Honda Accord| 1| |
| 69 | +| Dublin| Honda CRV| 1| |
| 70 | +|San Jose| Honda Civic| 1| |
| 71 | ++--------+------------+----+ |
| 72 | + |
| 73 | +-- `QUALIFY` clause with a window function directly in the predicate. |
| 74 | +SELECT city, car_model |
| 75 | +FROM dealer |
| 76 | +QUALIFY RANK() OVER (PARTITION BY car_model ORDER BY quantity) = 1; |
| 77 | ++--------+------------+ |
| 78 | +| city| car_model| |
| 79 | ++--------+------------+ |
| 80 | +|San Jose|Honda Accord| |
| 81 | +| Dublin| Honda CRV| |
| 82 | +|San Jose| Honda Civic| |
| 83 | ++--------+------------+ |
| 84 | +``` |
| 85 | + |
| 86 | +### Related Statements |
| 87 | + |
| 88 | +* [SELECT Main](sql-ref-syntax-qry-select.html) |
| 89 | +* [WHERE Clause](sql-ref-syntax-qry-select-where.html) |
| 90 | +* [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html) |
| 91 | +* [HAVING Clause](sql-ref-syntax-qry-select-having.html) |
| 92 | +* [WINDOW Clause](sql-ref-syntax-qry-select-window.html) |
| 93 | +* [ORDER BY Clause](sql-ref-syntax-qry-select-orderby.html) |
| 94 | +* [SORT BY Clause](sql-ref-syntax-qry-select-sortby.html) |
| 95 | +* [CLUSTER BY Clause](sql-ref-syntax-qry-select-clusterby.html) |
| 96 | +* [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html) |
| 97 | +* [LIMIT Clause](sql-ref-syntax-qry-select-limit.html) |
| 98 | +* [OFFSET Clause](sql-ref-syntax-qry-select-offset.html) |
0 commit comments