# uniq (/docs/reference/sql/aggregate-functions/uniq)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

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.

<details>
  <summary>
    Implementation details
  </summary>

  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.
</details>

:::tip
We recommend using this function over other variants in almost all scenarios.
:::

**Syntax**

```sql
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
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;
```

```text title=Response
┌─unique_categories─┐
│                 3 │
└───────────────────┘
```

**Multiple arguments**

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

```text title=Response
┌─unique_combinations─┐
│                   6 │
└─────────────────────┘
```

*Introduced in version 1.1.0.*
