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

# `hll_union` 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 combine two sketches into a single sketch.

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

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_union ( expr1, expr2 [, allowDifferentLgConfigK ] )
```

## Arguments

* `exprN`: A `BINARY` expression holding a sketch generated by [hll\_sketch\_agg](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_agg.md).
* `allowDifferentLgConfigK`: A optional `BOOLEAN` expression controlling whether to allow merging two sketches with different lgConfigK values. The default value is false.

## Returns

A `BINARY` buffer containing the HyperLogLog sketch computed as a result of combining the input expressions.

When the `allowDifferentLgConfigK` parameter is true, the result sketch uses the smaller of the two provided `lgConfigK` values.

## Examples

```sql
> SELECT hll_sketch_estimate(
  hll_union(
    hll_sketch_agg(col1),
    hll_sketch_agg(col2)))
  FROM VALUES
    (1, 4),
    (1, 4),
    (2, 5),
    (2, 5),
    (3, 6) AS tab(col1, col2);
  6

> SELECT hll_sketch_estimate(
  hll_union(
    hll_sketch_agg(col1,  4),
    hll_sketch_agg(col2, 21)))
  FROM VALUES
    (1, 4),
    (1, 4),
    (2, 5),
    (2, 5),
    (3, 6) AS tab(col1, col2);
  error
```

## Related

* [`hll_sketch_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_agg.md)
* [`hll_sketch_estimate` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_estimate.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)
