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

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

# topK

<h2 id="topK">
  topK
</h2>

Introduced in: v1.1.0

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 theme={null}
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 theme={null}
SELECT topK(3)(AirlineID) AS res
FROM ontime;
```

```response title=Response theme={null}
┌─res─────────────────┐
│ [19393,19790,19805] │
└─────────────────────┘
```

**See Also**

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