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

> Counts the number of rows or not-NULL values.

# count

<h2 id="count">
  count
</h2>

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`](/reference/settings/session-settings#count_distinct_implementation) setting.
It defines which of the [uniq\*](/reference/functions/aggregate-functions/uniq) functions is used to perform the operation.
The default is the [uniqExact](/reference/functions/aggregate-functions/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`](/reference/settings/session-settings#optimize_trivial_count_query) setting.

However `SELECT count(nullable_column) FROM table` query can be optimized by enabling the [`optimize_functions_to_subcolumns`](/reference/settings/session-settings#optimize_functions_to_subcolumns) setting.
With `optimize_functions_to_subcolumns = 1` the function reads only [`null`](/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`.

<Tip>
  **Improving COUNT(DISTINCT expr) performance**

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

**Syntax**

```sql theme={null}
count([expr])
```

**Arguments**

* `expr` — Optional. An expression. The function counts how many times this expression returned not null. [`Expression`](/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`](/reference/data-types/int-uint)

**Examples**

**Basic row count**

```sql title=Query theme={null}
SELECT count() FROM t
```

```response title=Response theme={null}
┌─count()─┐
│       5 │
└─────────┘
```

**COUNT(DISTINCT) example**

```sql title=Query theme={null}
-- 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
```

```response title=Response theme={null}
┌─name──────────────────────────┬─value─────┐
│ count_distinct_implementation │ uniqExact │
└───────────────────────────────┴───────────┘
┌─uniqExact(num)─┐
│              3 │
└────────────────┘
```
