# topK (/docs/reference/sql/aggregate-functions/topk)



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

Returns an array of the approximately most frequent values in the specified column. The resulting array is sorted in descending order of approximate frequency of values (not by the values themselves).

Implements the [Filtered Space-Saving](https://doi.org/10.1016/j.ins.2010.08.024) algorithm for analyzing TopK, based on the reduce-and-combine algorithm from [Parallel Space Saving](https://doi.org/10.1016/j.ins.2015.09.003).

This function does not provide a guaranteed result. In certain situations, errors might occur and it might return frequent values that aren't the most frequent values.

**See Also**

* [topKWeighted](/reference/functions/aggregate-functions/topKWeighted)
* [approx\_top\_k](/reference/functions/aggregate-functions/approxtopk)
* [approx\_top\_sum](/reference/functions/aggregate-functions/approxtopsum)

**Syntax**

```sql
topK(N)(column)
topK(N, load_factor)(column)
topK(N, load_factor, 'counts')(column)
```

**Parameters**

* `N` — The number of elements to return. Default value: 10. Maximum value of `N = 65536`. [`UInt64`](/reference/data-types/int-uint)
* `load_factor` — Optional. Defines, how many cells reserved for values. If `uniq(column) > N * load_factor`, result of topK function will be approximate. Default value: 3. [`UInt64`](/reference/data-types/int-uint)
* `counts` — Optional. Defines whether the result should contain an approximate count and error value. [`Bool`](/reference/data-types/boolean)

**Arguments**

* `column` — The name of the column for which to find the most frequent values. [`String`](/reference/data-types/string)

**Returned value**

Returns an array of the approximately most frequent values, sorted in descending order of approximate frequency. [`Array`](/reference/data-types/array)

**Examples**

**Usage example**

```sql title=Query
SELECT topK(3)(AirlineID) AS res
FROM ontime;
```

```text title=Response
┌─res─────────────────┐
│ [19393,19790,19805] │
└─────────────────────┘
```

*Introduced in version 1.1.0.*
