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

> Computes the quantile of a numeric data sequence using the Greenwald-Khanna algorithm.

# quantileGK

<h2 id="quantileGK">
  quantileGK
</h2>

Introduced in: v23.4.0

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

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.
It is widely used in databases and big data systems where computing accurate quantiles on a large stream of data in real-time is necessary.
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 an approximate quantile value with high probability.

`quantileGK` is different from other quantile functions in ClickHouse, because it enables user to control the accuracy of the approximate quantile result.

**Syntax**

```sql theme={null}
quantileGK(accuracy, level)(expr)
```

**Aliases**: `medianGK`

**Parameters**

* `accuracy` — Accuracy of quantile. Constant positive integer. Larger accuracy value means less error. For example, if the accuracy argument is set to 100, the computed quantile 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. A larger accuracy requires more memory and computational resources to compute the quantile accurately, while a smaller accuracy argument allows for a faster and more memory-efficient computation but with a slightly lower accuracy. [`UInt*`](/reference/data-types/int-uint)
* `level` — Optional. Level of quantile. Constant floating-point number from 0 to 1. Default value: 0.5. At `level=0.5` the function calculates median. [`Float*`](/reference/data-types/float)

**Arguments**

* `expr` — Expression over the column values resulting in numeric data types, Date or DateTime. [`(U)Int*`](/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)

**Returned value**

Returns the quantile of the specified level and accuracy. [`Float64`](/reference/data-types/float) or [`Date`](/reference/data-types/date) or [`DateTime`](/reference/data-types/datetime)

**Examples**

**Computing quantile with different accuracy levels**

```sql title=Query theme={null}
SELECT quantileGK(1, 0.25)(number + 1) FROM numbers(1000);
```

```response title=Response theme={null}
┌─quantileGK(1, 0.25)(plus(number, 1))─┐
│                                    1 │
└──────────────────────────────────────┘
```

**Higher accuracy quantile**

```sql title=Query theme={null}
SELECT quantileGK(100, 0.25)(number + 1) FROM numbers(1000);
```

```response title=Response theme={null}
┌─quantileGK(100, 0.25)(plus(number, 1))─┐
│                                    251 │
└────────────────────────────────────────┘
```

**See Also**

* [median](/reference/functions/aggregate-functions/median)
* [quantiles](/reference/functions/aggregate-functions/quantiles)
