---
title: QUALIFY clause
description: Filter the results of window functions with the QUALIFY clause in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-qualify
---

# QUALIFY clause

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 10.4 LTS and above.

Filters the results of window functions. To use `QUALIFY`, at least one window function is required to be present in the `SELECT` list or the `QUALIFY` clause.

## Syntax

```
QUALIFY boolean_expression
```

## Parameters

* **[boolean\_expression](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-expression.md)**

  Any expression that evaluates to a result type `boolean`. Two or
  more expressions may be combined together using the logical
  operators ( [AND](https://docs.databricks.com/aws/en/sql/language-manual/functions/and.md), [OR](https://docs.databricks.com/aws/en/sql/language-manual/functions/or.md)).

  The expressions specified in the `QUALIFY` clause cannot contain aggregate functions.

## Examples

```sql
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 with window functions in the SELECT list.
> 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 with window functions in the QUALIFY clause.
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
```

## Related statements

* [`SELECT` (subselect)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md)
* [WHERE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-where.md)
* [GROUP BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-groupby.md)
* [ORDER BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-orderby.md)
* [SORT BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sortby.md)
* [CLUSTER BY clause (SELECT)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-clusterby.md)
* [DISTRIBUTE BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-distributeby.md)
* [LIMIT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-limit.md)
* [PIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pivot.md)
* [LATERAL VIEW clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-lateral-view.md)
