> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Calculates the approximate number of different values of the argument.

# uniq

<h2 id="uniq">
  uniq
</h2>

Introduced in: v1.1.0

Calculates the approximate number of different values of the argument.

The function uses an adaptive sampling algorithm. For the calculation state, the function uses a sample of element hash values up to 65536. This algorithm is very accurate and very efficient on the CPU. When the query contains several of these functions, using uniq is almost as fast as using other aggregate functions.

<Accordion title="Implementation details">
  This function calculates a hash for all parameters in the aggregate, then uses it in calculations.
  It uses an adaptive sampling algorithm.
  For the calculation state, the function uses a sample of element hash values up to 65536.
  This algorithm is very accurate and very efficient on the CPU.
  When the query contains several of these functions, using `uniq` is almost as fast as using other aggregate functions.

  The sample keeps 32-bit hashes, which cannot distinguish more than a few billion values, so in addition
  a small heavily-thinned-out sample of full 64-bit hashes is maintained (about one hash per 262144 values,
  bounded by 16384 hashes), and the estimate switches to it at around two billion. This keeps the estimate
  accurate (within about 1%) for cardinalities of hundreds of billions and more, where servers older
  than 26.8 returned garbage (issue #6078). States written by those servers carry no 64-bit sample,
  so merging with them keeps only the 32-bit precision for the merged-in values.
</Accordion>

<Tip>
  We recommend using this function over other variants in almost all scenarios.
</Tip>

**Syntax**

```sql theme={null}
uniq(x[, ...])
```

**Arguments**

* `x` — The function takes a variable number of parameters. [`Tuple(T)`](/reference/data-types/tuple) or [`Array(T)`](/reference/data-types/array) or [`Date`](/reference/data-types/date) or [`DateTime`](/reference/data-types/datetime) or [`String`](/reference/data-types/string) or [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/reference/data-types/decimal)

**Returned value**

Returns a UInt64-type number representing the approximate number of different values. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Example usage**

```sql title=Query theme={null}
CREATE TABLE example_table (
    id UInt32,
    category String,
    value Float64
) ENGINE = Memory;

INSERT INTO example_table VALUES
(1, 'A', 10.5),
(2, 'B', 20.3),
(3, 'A', 15.7),
(4, 'C', 8.9),
(5, 'B', 12.1),
(6, 'A', 18.4);

SELECT uniq(category) as unique_categories
FROM example_table;
```

```response title=Response theme={null}
┌─unique_categories─┐
│                 3 │
└───────────────────┘
```

**Multiple arguments**

```sql title=Query theme={null}
SELECT uniq(category, value) as unique_combinations
FROM example_table;
```

```response title=Response theme={null}
┌─unique_combinations─┐
│                   6 │
└─────────────────────┘
```

**See Also**

* [uniqCombined](/reference/functions/aggregate-functions/uniqCombined)
* [uniqCombined64](/reference/functions/aggregate-functions/uniqCombined64)
* [uniqHLL12](/reference/functions/aggregate-functions/uniqHLL12)
* [uniqExact](/reference/functions/aggregate-functions/uniqExact)
* [uniqTheta](/reference/functions/aggregate-functions/uniqthetasketch)
