---
title: count_if aggregate function
description: Learn the syntax of the count\_if aggregate function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/count_if
---

# `count_if` aggregate function

**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

Returns the number of true values for the group in `expr`.

## Syntax

```
count_if ( [ALL | DISTINCT] expr ) [ FILTER ( WHERE cond ) ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: A BOOLEAN expression.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

A `BIGINT`.

`count_if(expr) FILTER(WHERE cond)` is equivalent to `count_if(expr AND cond)`.

If `DISTINCT` is specified only unique rows are counted.

## Examples

```sql
> SELECT count_if(col % 2 = 0) FROM VALUES (NULL), (0), (1), (2), (2), (3) AS tab(col);
 3

> SELECT count_if(DISTINCT col % 2 = 0) FROM VALUES (NULL), (0), (1), (2), (2), (3) AS tab(col);
 2

> SELECT count_if(col IS NULL) FROM VALUES (NULL), (0), (1), (2), (3) AS tab(col);
 1
```

## Related

* [`avg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/avg.md)
* [`sum` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/sum.md)
* [`min` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/min.md)
* [`max` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/max.md)
* [`count` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/count.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
