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

# `hll_sketch_agg` 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 13.3 LTS and above

This function utilizes the [HyperLogLog](https://en.wikipedia.org/wiki/HyperLogLog) algorithm to count a probabilistic approximation of the number of unique values in a given column, and outputs the result as a binary representation known as a sketch buffer.
This binary representation is suitable for persistence.

Queries can use the resulting buffers to compute approximate unique counts with the [hll\_sketch\_estimate](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_estimate.md) function.

The [hll\_union](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union.md) and [hll\_union\_agg](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union_agg.md) functions can also combine sketches together by consuming and merging these buffers as inputs.

The implementation uses the [Apache Datasketches library](https://datasketches.apache.org). Please see [HLL](https://datasketches.apache.org/docs/HLL/HllJavaExample.html) for more information.

## Syntax

```
hll_sketch_agg ( expr [, lgConfigK ] )
```

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`: An expression of type `INT`, `BIGINT`, `STRING`, or `BINARY` against which unique counting will occur.
* `lgConfigK`: An optional `INT` constant between 4 and 21 inclusive with default 12. The log-base-2 of K, where K is the number of buckets or slots for the sketch.

Any `NULL` in `expr` is ignored.

## Returns

A non-`NULL` `BINARY` buffer containing the HyperLogLog sketch computed as a result of consuming and aggregating all input values in the aggregation group.

Databricks raises [HLL\_INVALID\_LG\_K](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_invalid_lg_k) if `lgConfigK` is not between 4 and 21.

Databricks raises [HLL\_K\_MUST\_BE\_CONSTANT](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_k_must_be_constant) if `lgConfigK` is not a constant.

## Common error conditions

* [HLL\_INVALID\_LG\_K](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_invalid_lg_k)
* [HLL\_K\_MUST\_BE\_CONSTANT](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_k_must_be_constant)

## Examples

```sql
> SELECT hll_sketch_estimate(hll_sketch_agg(col, 12))
    FROM VALUES (1), (1), (2), (2), (3) tab(col);
  3

> SELECT hll_sketch_estimate(hll_sketch_agg(col))
    FROM VALUES (1), (1), (2), (2), (3) tab(col);
  3

> SELECT hll_sketch_agg(col, 0) FROM VALUES (1) AS tab(col);
  Error: HLL_INVALID_LG_K
```

## Related

* [`hll_sketch_estimate` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_estimate.md)
* [`hll_union` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union.md)
* [`hll_union_agg` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union_agg.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
