# quantilesGK (/docs/reference/sql/aggregate-functions/quantilesgk)



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).

Computes multiple [quantiles](https://en.wikipedia.org/wiki/Quantile) of a numeric data sequence at different levels simultaneously using the [Greenwald-Khanna](http://infolab.stanford.edu/~datar/courses/cs361a/papers/quantiles.pdf) algorithm.

This function works similarly with [`quantileGK`](/reference/functions/aggregate-functions/quantileGK) but allows computing multiple quantile levels in a single pass, which is more efficient than calling individual quantile functions.

The Greenwald-Khanna algorithm is an algorithm used to compute quantiles on a stream of data in a highly efficient manner.
It was introduced by Michael Greenwald and Sanjeev Khanna in 2001.
The algorithm is highly efficient, taking only O(log n) space and O(log log n) time per item (where n is the size of the input).
It is also highly accurate, providing approximate quantile values with controllable accuracy.

**Syntax**

```sql
quantilesGK(accuracy, level1, level2, ...)(expr)
```

**Parameters**

* `accuracy` — Accuracy of quantiles. Constant positive integer. Larger accuracy value means less error. For example, if the accuracy argument is set to 100, the computed quantiles will have an error no greater than 1% with high probability. There is a trade-off between the accuracy of the computed quantiles and the computational complexity of the algorithm. [`UInt*`](/reference/data-types/int-uint)
* `level` — Levels of quantiles. One or more constant floating-point numbers from 0 to 1. [`Float*`](/reference/data-types/float)

**Arguments**

* `expr` — Expression over the column values resulting in numeric data types, `Date`, `DateTime` or `DateTime64`. [`(U)Int*`](/reference/data-types/int-uint) or [`Int128`](/reference/data-types/int-uint) or [`UInt128`](/reference/data-types/int-uint) or [`Int256`](/reference/data-types/int-uint) or [`UInt256`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`Date`](/reference/data-types/date) or [`DateTime`](/reference/data-types/datetime) or [`DateTime64`](/reference/data-types/datetime64)

**Returned value**

Array of quantiles of the specified levels in the same order as the levels were specified. For numeric data types the output format matches the input format. [`Array((U)Int*)`](/reference/data-types/array) or [`Array(Int128)`](/reference/data-types/array) or [`Array(UInt128)`](/reference/data-types/array) or [`Array(Int256)`](/reference/data-types/array) or [`Array(UInt256)`](/reference/data-types/array) or [`Array(Float*)`](/reference/data-types/array) or [`Array(Decimal*)`](/reference/data-types/array) or [`Array(Date)`](/reference/data-types/array) or [`Array(DateTime)`](/reference/data-types/array) or [`Array(DateTime64)`](/reference/data-types/array)

**Examples**

**Computing multiple quantiles with GK algorithm**

```sql title=Query
SELECT quantilesGK(1, 0.25, 0.5, 0.75)(number + 1) FROM numbers(1000);
```

```text title=Response
┌─quantilesGK(1, 0.25, 0.5, 0.75)(plus(number, 1))─┐
│ [1, 1, 1]                                        │
└──────────────────────────────────────────────────┘
```

**Higher accuracy quantiles**

```sql title=Query
SELECT quantilesGK(100, 0.25, 0.5, 0.75)(number + 1) FROM numbers(1000);
```

```text title=Response
┌─quantilesGK(100, 0.25, 0.5, 0.75)(plus(number, 1))─┐
│ [251, 498, 741]                                    │
└────────────────────────────────────────────────────┘
```

*Introduced in version 23.4.0.*
