ClickHouse/clickhouse-docs-embeddings huggingface dataset:
argAndMin

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/argAndMin.md)

## argAndMin[​](#argAndMin "Direct link to argAndMin")

Introduced in: v1.1.0

Calculates the `arg` and `val` value for a minimum `val` value.
If there are multiple rows with equal `val` being the minimum, which of the associated `arg` and `val` is returned is not deterministic.
Both parts the `arg` and the `min` behave as [aggregate functions](/docs/sql-reference/aggregate-functions), they both [skip `Null`](/docs/sql-reference/aggregate-functions#null-processing) during processing and return not `Null` values if not `Null` values are available.

Note

The only difference with `argMin` is that `argAndMin` returns both argument and value.

**See also**

- [argMin](/docs/sql-reference/aggregate-functions/reference/argmin)
- [Tuple](/docs/sql-reference/data-types/tuple)

**Syntax**

```
argAndMin(arg, val)
```

**Arguments**

- `arg` — Argument for which to find the minimum value. [`const String`](/docs/sql-reference/data-types/string)
- `val` — The minimum value. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Date`](/docs/sql-reference/data-types/date) or [`DateTime`](/docs/sql-reference/data-types/datetime) or [`Tuple`](/docs/sql-reference/data-types/tuple)

**Returned value**

Returns a tuple containing the `arg` value that corresponds to minimum `val` value and the minimum `val` value. [`Tuple`](/docs/sql-reference/data-types/tuple)

**Examples**

**Basic usage**

```
SELECT argAndMin(user, salary) FROM salary;
```

```
┌─argAndMin(user, salary)─┐
│ ('worker',1000)         │
└─────────────────────────┘
```

**Extended example with NULL handling**

```
CREATE TABLE test
(
    a Nullable(String),
    b Nullable(Int64)
)
ENGINE = Memory AS
SELECT *
FROM VALUES((NULL, 0), ('a', 1), ('b', 2), ('c', 2), (NULL, NULL), ('d', NULL));

SELECT argMin(a,b), argAndMin(a, b), min(b) FROM test;
```

```
┌─argMin(a, b)─┬─argAndMin(a, b)─┬─min(b)─┐
│ a            │ ('a',1)         │      0 │
└──────────────┴─────────────────┴────────┘
```

**Using Tuple in arguments**

```
SELECT argAndMin(a, (b, a)), min(tuple(b, a)) FROM test;
```

```
┌─argAndMin(a, (b, a))─┬─min((b, a))─┐
│ ('a',(1,'a'))        │ (0,NULL)    │
└──────────────────────┴─────────────┘
```

**See also**

- [argMin](/docs/sql-reference/aggregate-functions/reference/argmin)
- [Tuple](/docs/sql-reference/data-types/tuple)

- [argAndMin](#argAndMin)

Was this page helpful?
argMax

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/argMax.md)

## argMax[​](#argMax "Direct link to argMax")

Introduced in: v1.1.0

Calculates the `arg` value for a maximum `val` value. If there are multiple rows with equal `val` being the maximum, which of the associated `arg` is returned is not deterministic.
Both parts the `arg` and the `max` behave as [aggregate functions](/docs/sql-reference/aggregate-functions), they both [skip `Null`](/docs/sql-reference/aggregate-functions#null-processing) during processing and return not `Null` values if not `Null` values are available.

**See also**

- [Tuple](/docs/sql-reference/data-types/tuple)

**Syntax**

```
argMax(arg, val)
```

**Arguments**

- `arg` — Argument for which to find the maximum value. [`const String`](/docs/sql-reference/data-types/string)
- `val` — The maximum value. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Date`](/docs/sql-reference/data-types/date) or [`DateTime`](/docs/sql-reference/data-types/datetime) or [`Tuple`](/docs/sql-reference/data-types/tuple)

**Returned value**

Returns the `arg` value that corresponds to maximum `val` value. Type matches `arg` type.

**Examples**

**Basic usage**

```
SELECT argMax(user, salary) FROM salary;
```

```
┌─argMax(user, salary)─┐
│ director             │
└──────────────────────┘
```

**Extended example with NULL handling**

```
CREATE TABLE test
(
    a Nullable(String),
    b Nullable(Int64)
)
ENGINE = Memory AS
SELECT *
FROM VALUES(('a', 1), ('b', 2), ('c', 2), (NULL, 3), (NULL, NULL), ('d', NULL));

SELECT argMax(a, b), max(b) FROM test;
```

```
┌─argMax(a, b)─┬─max(b)─┐
│ b            │      3 │
└──────────────┴────────┘
```

**Using Tuple in arguments**

```
SELECT argMax(a, (b,a)) FROM test;
```

```
┌─argMax(a, tuple(b, a))─┐
│ c                      │
└────────────────────────┘
```

- [argMax](#argMax)

Was this page helpful?
argMin

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/argMin.md)

## argMin[​](#argMin "Direct link to argMin")

Introduced in: v1.1.0

Calculates the `arg` value for a minimum `val` value. If there are multiple rows with equal `val` being the maximum, which of the associated `arg` is returned is not deterministic.
Both parts the `arg` and the `min` behave as [aggregate functions](/docs/sql-reference/aggregate-functions), they both [skip `Null`](/docs/sql-reference/aggregate-functions#null-processing) during processing and return not `Null` values if not `Null` values are available.

**See also**

- [Tuple](/docs/sql-reference/data-types/tuple)

**Syntax**

```
argMin(arg, val)
```

**Arguments**

- `arg` — Argument for which to find the maximum value. [`const String`](/docs/sql-reference/data-types/string)
- `val` — The minimum value. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Date`](/docs/sql-reference/data-types/date) or [`DateTime`](/docs/sql-reference/data-types/datetime) or [`Tuple`](/docs/sql-reference/data-types/tuple)

**Returned value**

Returns the `arg` value that corresponds to minimum `val` value. Type matches `arg` type.

**Examples**

**Basic usage**

```
SELECT argMin(user, salary) FROM salary;
```

```
┌─argMin(user, salary)─┐
│ worker               │
└──────────────────────┘
```

**Extended example with NULL handling**

```
CREATE TABLE test
(
    a Nullable(String),
    b Nullable(Int64)
)
ENGINE = Memory AS
SELECT *
FROM VALUES((NULL, 0), ('a', 1), ('b', 2), ('c', 2), (NULL, NULL), ('d', NULL));

SELECT argMin(a, b), min(b) FROM test;
```

```
┌─argMin(a, b)─┬─min(b)─┐
│ a            │      0 │
└──────────────┴────────┘
```

**Using Tuple in arguments**

```
SELECT argMin(a, (b, a)), min(tuple(b, a)) FROM test;
```

```
┌─argMin(a, tuple(b, a))─┬─min(tuple(b, a))─┐
│ d                      │ (NULL,NULL)      │
└────────────────────────┴──────────────────┘
```

- [argMin](#argMin)

Was this page helpful?
avg

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/avg.md)

## avg[​](#avg "Direct link to avg")

Introduced in: v1.1.0

Calculates the arithmetic mean.

**Syntax**

```
avg(x)
```

**Arguments**

- `x` — Input values. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the arithmetic mean, otherwise returns `NaN` if the input parameter `x` is empty. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT avg(x) FROM VALUES('x Int8', 0, 1, 2, 3, 4, 5);
```

```
┌─avg(x)─┐
│    2.5 │
└────────┘
```

**Empty table returns NaN**

```
CREATE TABLE test (t UInt8) ENGINE = Memory;

SELECT avg(t) FROM test;
```

```
┌─avg(x)─┐
│    nan │
└────────┘
```

- [avg](#avg)

Was this page helpful?
avgWeighted

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/avgWeighted.md)

## avgWeighted[​](#avgWeighted "Direct link to avgWeighted")

Introduced in: v20.1.0

Calculates the [weighted arithmetic mean](https://en.wikipedia.org/wiki/Weighted_arithmetic_mean).

**Syntax**

```
avgWeighted(x, weight)
```

**Arguments**

- `x` — Values. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `weight` — Weights of the values. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns `NaN` if all the weights are equal to 0 or the supplied weights parameter is empty, or the weighted mean otherwise. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Usage example**

```
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (4, 1), (1, 0), (10, 2))
```

```
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
```

**Mixed integer and float weights**

```
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Float64', (4, 1), (1, 0), (10, 2))
```

```
┌─avgWeighted(x, w)─┐
│                 8 │
└───────────────────┘
```

**All weights are zero returns NaN**

```
SELECT avgWeighted(x, w)
FROM VALUES('x Int8, w Int8', (0, 0), (1, 0), (10, 0))
```

```
┌─avgWeighted(x, w)─┐
│               nan │
└───────────────────┘
```

**Empty table returns NaN**

```
CREATE TABLE test (t UInt8) ENGINE = Memory;
SELECT avgWeighted(t, t) FROM test
```

```
┌─avgWeighted(t, t)─┐
│               nan │
└───────────────────┘
```

- [avgWeighted](#avgWeighted)

Was this page helpful?
boundingRatio

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/boundingRatio.md)

## boundingRatio[​](#boundingRatio "Direct link to boundingRatio")

Introduced in: v20.1.0

Calculates the slope between the leftmost and rightmost points across a group of values.

**Syntax**

```
boundingRatio(x, y)
```

**Arguments**

- `x` — X-coordinate values. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)
- `y` — Y-coordinate values. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the slope of the line between the leftmost and rightmost points, otherwise returns `NaN` if the data is empty. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Sample data**

```
SELECT
    number,
    number * 1.5
FROM numbers(10)
```

```
┌─number─┬─multiply(number, 1.5)─┐
│      0 │                     0 │
│      1 │                   1.5 │
│      2 │                     3 │
│      3 │                   4.5 │
│      4 │                     6 │
│      5 │                   7.5 │
│      6 │                     9 │
│      7 │                  10.5 │
│      8 │                    12 │
│      9 │                  13.5 │
└────────┴───────────────────────┘
```

**Usage example**

```
SELECT boundingRatio(number, number * 1.5)
FROM numbers(10)
```

```
┌─boundingRatio(number, multiply(number, 1.5))─┐
│                                          1.5 │
└──────────────────────────────────────────────┘
```

- [boundingRatio](#boundingRatio)

Was this page helpful?
categoricalInformationValue

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/categoricalInformationValue.md)

## categoricalInformationValue[​](#categoricalInformationValue "Direct link to categoricalInformationValue")

Introduced in: v20.1.0

Calculates the information value (IV) for categorical features in relation to a binary target variable.

For each category, the function computes: `(P(tag = 1) - P(tag = 0)) × (log(P(tag = 1)) - log(P(tag = 0)))`

where:

- P(tag = 1) is the probability that the target equals 1 for the given category
- P(tag = 0) is the probability that the target equals 0 for the given category

Information Value is a statistic used to measure the strength of a categorical feature's relationship with a binary target variable in predictive modeling.
Higher absolute values indicate stronger predictive power.

The result indicates how much each discrete (categorical) feature `[category1, category2, ...]` contributes to a learning model which predicts the value of `tag`.

**Syntax**

```
categoricalInformationValue(category1[, category2, ...,]tag)
```

**Arguments**

- `category1, category2, ...` — One or more categorical features to analyze. Each category should contain discrete values. [`UInt8`](/docs/sql-reference/data-types/int-uint)
- `tag` — Binary target variable for prediction. Should contain values 0 and 1. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array of Float64 values representing the information value for each unique combination of categories. Each value indicates the predictive strength of that category combination for the target variable. [`Array(Float64)`](/docs/sql-reference/data-types/array)

**Examples**

**Basic usage analyzing age groups vs mobile usage**

```
-- Using the metrica.hits dataset (available on https://sql.clickhouse.com/) to analyze age-mobile relationship
SELECT categoricalInformationValue(Age < 15, IsMobile)
FROM metrica.hits;
```

```
[0.0014814694805292418]
```

**Multiple categorical features with user demographics**

```
SELECT categoricalInformationValue(
    Sex,                 -- 0=male, 1=female
    toUInt8(Age < 25),   -- 0=25+, 1=under 25
    toUInt8(IsMobile)    -- 0=desktop, 1=mobile
) AS iv_values
FROM metrica.hits
WHERE Sex IN (0, 1);
```

```
[0.00018965785460692887,0.004973668839403392]
```

- [categoricalInformationValue](#categoricalInformationValue)

Was this page helpful?
contingency

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/contingency.md)

## contingency[​](#contingency "Direct link to contingency")

Introduced in: v22.1.0

The `contingency` function calculates the [contingency coefficient](https://en.wikipedia.org/wiki/Contingency_table#Cram%C3%A9r's_V_and_the_contingency_coefficient_C), a value that measures the association between two columns in a table.
The computation is similar to the [`cramersV`](/docs/sql-reference/aggregate-functions/reference/cramersv) function but with a different denominator in the square root.

**Syntax**

```
contingency(column1, column2)
```

**Arguments**

- `column1` — First column to compare. [`Any`](/docs/sql-reference/data-types)
- `column2` — Second column to compare. [`Any`](/docs/sql-reference/data-types)

**Returned value**

Returns a value between 0 and 1. The larger the result, the closer the association of the two columns. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Comparison with cramersV**

```
SELECT
    cramersV(a, b),
    contingency(a, b)
FROM
(
    SELECT
        number % 10 AS a,
        number % 4 AS b
    FROM
        numbers(150)
)
```

```
┌──────cramersV(a, b)─┬───contingency(a, b)─┐
│ 0.41171788506213564 │ 0.05812725261759165 │
└─────────────────────┴─────────────────────┘
```

- [contingency](#contingency)

Was this page helpful?
corr

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/corr.md)

## corr[​](#corr "Direct link to corr")

Introduced in: v1.1.0

Calculates the [Pearson correlation coefficient](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient):

Σ(x−xˉ)(y−yˉ)Σ(x−xˉ)2∗Σ(y−yˉ)2\frac{\Sigma{(x - \bar{x})(y - \bar{y})}}{\sqrt{\Sigma{(x - \bar{x})^2} \* \Sigma{(y - \bar{y})^2}}}Σ(x−xˉ)2∗Σ(y−yˉ​)2​Σ(x−xˉ)(y−yˉ​)​
  

Note

This function uses a numerically unstable algorithm. If you need [numerical stability](https://en.wikipedia.org/wiki/Numerical_stability) in calculations, use the [`corrStable`](/docs/sql-reference/aggregate-functions/reference/corrstable) function. It is slower but provides a more accurate result.

**Syntax**

```
corr(x, y)
```

**Arguments**

- `x` — First variable. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)
- `y` — Second variable. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the Pearson correlation coefficient. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Basic correlation calculation**

```
DROP TABLE IF EXISTS series;
CREATE TABLE series
(
    i UInt32,
    x_value Float64,
    y_value Float64
)
ENGINE = Memory;
INSERT INTO series(i, x_value, y_value) VALUES (1, 5.6, -4.4),(2, -9.6, 3),(3, -1.3, -4),(4, 5.3, 9.7),(5, 4.4, 0.037),(6, -8.6, -7.8),(7, 5.1, 9.3),(8, 7.9, -3.6),(9, -8.2, 0.62),(10, -3, 7.3);

SELECT corr(x_value, y_value)
FROM series
```

```
┌─corr(x_value, y_value)─┐
│     0.1730265755453256 │
└────────────────────────┘
```

- [corr](#corr)

Was this page helpful?
corrMatrix

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/corrMatrix.md)

## corrMatrix[​](#corrMatrix "Direct link to corrMatrix")

Introduced in: v23.2.0

Computes the correlation matrix over N variables.

**Syntax**

```
corrMatrix(x1[, x2, ...])
```

**Arguments**

- `x1[, x2, ...]` — One or more parameters for which to compute the correlation matrix over. [`(U)Int8/16/32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the correlation matrix. [`Array(Array(Float64))`](/docs/sql-reference/data-types/array)

**Examples**

**Basic correlation matrix calculation**

```
DROP TABLE IF EXISTS test;
CREATE TABLE test
(
    a UInt32,
    b Float64,
    c Float64,
    d Float64
)
ENGINE = Memory;
INSERT INTO test(a, b, c, d) VALUES (1, 5.6, -4.4, 2.6), (2, -9.6, 3, 3.3), (3, -1.3, -4, 1.2), (4, 5.3, 9.7, 2.3), (5, 4.4, 0.037, 1.222), (6, -8.6, -7.8, 2.1233), (7, 5.1, 9.3, 8.1222), (8, 7.9, -3.6, 9.837), (9, -8.2, 0.62, 8.43555), (10, -3, 7.3, 6.762);

SELECT arrayMap(x -> round(x, 3), arrayJoin(corrMatrix(a, b, c, d))) AS corrMatrix
FROM test
```

```
┌─corrMatrix─────────────┐
│ [1,-0.096,0.243,0.746] │
│ [-0.096,1,0.173,0.106] │
│ [0.243,0.173,1,0.258]  │
│ [0.746,0.106,0.258,1]  │
└────────────────────────┘
```

- [corrMatrix](#corrMatrix)

Was this page helpful?
corrStable

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/corrStable.md)

## corrStable[​](#corrStable "Direct link to corrStable")

Introduced in: v1.1.0

Calculates the [Pearson correlation coefficient](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient):

Σ(x−xˉ)(y−yˉ)Σ(x−xˉ)2∗Σ(y−yˉ)2\frac{\Sigma{(x - \bar{x})(y - \bar{y})}}{\sqrt{\Sigma{(x - \bar{x})^2} \* \Sigma{(y - \bar{y})^2}}}Σ(x−xˉ)2∗Σ(y−yˉ​)2​Σ(x−xˉ)(y−yˉ​)​
  

Similar to the [`corr`](/docs/sql-reference/aggregate-functions/reference/corr) function, but uses a numerically stable algorithm.
As a result, `corrStable` is slower than `corr` but produces a more accurate result.

**Syntax**

```
corrStable(x, y)
```

**Arguments**

- `x` — First variable. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)
- `y` — Second variable. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the Pearson correlation coefficient. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Basic correlation calculation with stable algorithm**

```
DROP TABLE IF EXISTS series;
CREATE TABLE series
(
    i UInt32,
    x_value Float64,
    y_value Float64
)
ENGINE = Memory;
INSERT INTO series(i, x_value, y_value) VALUES (1, 5.6, -4.4),(2, -9.6, 3),(3, -1.3, -4),(4, 5.3, 9.7),(5, 4.4, 0.037),(6, -8.6, -7.8),(7, 5.1, 9.3),(8, 7.9, -3.6),(9, -8.2, 0.62),(10, -3, 7.3);

SELECT corrStable(x_value, y_value)
FROM series
```

```
┌─corrStable(x_value, y_value)─┐
│          0.17302657554532558 │
└──────────────────────────────┘
```

- [corrStable](#corrStable)

Was this page helpful?
count

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/count.md)

## count[​](#count "Direct link to count")

Introduced in: v1.1.0

Counts the number of rows or not-NULL values.

ClickHouse supports the following syntaxes for `count`:

- `count(expr)` or `COUNT(DISTINCT expr)`.
- `count()` or `COUNT(*)`. The `count()` syntax is ClickHouse-specific.

**Details**

ClickHouse supports the `COUNT(DISTINCT ...)` syntax.
The behavior of this construction depends on the [`count_distinct_implementation`](/docs/operations/settings/settings#count_distinct_implementation) setting.
It defines which of the [uniq\*](/docs/sql-reference/aggregate-functions/reference/uniq) functions is used to perform the operation.
The default is the [uniqExact](/docs/sql-reference/aggregate-functions/reference/uniqexact) function.

The `SELECT count() FROM table` query is optimized by default using metadata from MergeTree.
If you need to use row-level security, disable optimization using the [`optimize_trivial_count_query`](/docs/operations/settings/settings#optimize_trivial_count_query) setting.

However `SELECT count(nullable_column) FROM table` query can be optimized by enabling the [`optimize_functions_to_subcolumns`](/docs/operations/settings/settings#optimize_functions_to_subcolumns) setting.
With `optimize_functions_to_subcolumns = 1` the function reads only [`null`](/docs/sql-reference/data-types/nullable#finding-null) subcolumn instead of reading and processing the whole column data.
The query `SELECT count(n) FROM table` transforms to `SELECT sum(NOT n.null) FROM table`.

Improving COUNT(DISTINCT expr) performance

If your `COUNT(DISTINCT expr)` query is slow, consider adding a [`GROUP BY`](/docs/sql-reference/statements/select/group-by) clause as this improves parallelization.
You can also use a [projection](/docs/sql-reference/statements/alter/projection) to create an index on the target column used with `COUNT(DISTINCT target_col)`.

**Syntax**

```
count([expr])
```

**Arguments**

- `expr` — Optional. An expression. The function counts how many times this expression returned not null. [`Expression`](/docs/sql-reference/data-types/special-data-types/expression)

**Returned value**

Returns the a row count if the function is called without parameters, otherwise returns a count of how many times the passed expression returned not null. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Basic row count**

```
SELECT count() FROM t
```

```
┌─count()─┐
│       5 │
└─────────┘
```

**COUNT(DISTINCT) example**

```
-- This example shows that `count(DISTINCT num)` is performed by the `uniqExact` function according to the `count_distinct_implementation` setting value.
SELECT name, value FROM system.settings WHERE name = 'count_distinct_implementation';
SELECT count(DISTINCT num) FROM t
```

```
┌─name──────────────────────────┬─value─────┐
│ count_distinct_implementation │ uniqExact │
└───────────────────────────────┴───────────┘
┌─uniqExact(num)─┐
│              3 │
└────────────────┘
```

- [count](#count)

Was this page helpful?