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

# `approx_count_distinct` 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 estimated number of distinct values in `expr` within the group.

The implementation uses the dense version of the HyperLogLog++ (HLL++)
algorithm, a state of the art cardinality estimation algorithm.

Results are accurate within a default value of 5%, which derives from the value
of the maximum relative standard deviation, although this is configurable with
the `relativeSD` parameter as mentioned below.

## Syntax

```
approx_count_distinct(expr[, relativeSD]) [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`: Can be of any type for which equivalence is defined.
* `relativeSD`: Defines the maximum relative standard deviation allowed.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

A BIGINT.

## Examples

```sql
> SELECT approx_count_distinct(col1) FROM VALUES (1), (1), (2), (2), (3) tab(col1);
 3

> SELECT approx_count_distinct(col1) FILTER(WHERE col2 = 10)
    FROM VALUES (1, 10), (1, 10), (2, 10), (2, 10), (3, 10), (1, 12) AS tab(col1, col2);
 3
```

## Related functions

* [`approx_percentile` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/approx_percentile.md)
* [`approx_top_k` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/approx_top_k.md)
* [`histogram_numeric` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/histogram_numeric.md)
