ClickHouse Functions/Aggregate Functions

count

Count rows or non-NULL values — ClickHouse's fastest aggregate.

Signature

count() | count(column) | countIf(condition)

Returns

UInt64

What it does

Counts the number of rows (count()), non-NULL values in a column (count(col)), or rows matching a condition (countIf). The most common aggregate in analytics queries.

ClickHouse count() is highly optimised — for count() without a column argument it uses stored row counts and avoids data reading entirely. count(DISTINCT col) is available but use uniq() instead for approximate counts on large datasets as it is orders of magnitude faster. countIf(condition) avoids a subquery when you need conditional counts in a single pass.

Notes

  • count() (no argument) is the fastest — reads from metadata, not data files.
  • count(DISTINCT col) is exact but slow on high-cardinality columns. Use uniq(col) for approximations.
  • countIf(cond) = count() with a WHERE clause but runs in a single scan — useful for pivot-style queries.

Example SQL

count in ClickHouse SQL

count-example.sql

TypeScript with hypequery

Use count in a typed TypeScript query

hypequery gives you a type-safe query builder for ClickHouse. The generated schema maps your ClickHouse columns to TypeScript types, and raw SQL expressions let you incorporate functions like count when you need them inside a builder query.

event-counts.ts

Common questions

What developers search for with count

ClickHouse count rows TypeScript

In hypequery, execute a query and read .length on the result for row counts, or push count() into the raw SQL expression if you need it server-side.

countIf ClickHouse conditional count

countIf(condition) lets you calculate multiple conditional aggregates in a single table scan — faster than multiple subqueries.

FAQ

Frequently asked questions about count

What is the difference between count() and count(col)?

count() counts all rows including those with NULLs. count(col) counts only rows where col is not NULL.

Should I use count(DISTINCT col) or uniq(col)?

Use uniq(col) for large datasets — it uses a HyperLogLog-based approximation that is much faster and uses less memory. count(DISTINCT col) is exact but slow.

Related functions

Functions used alongside count

Next step

Use count in a type-safe TypeScript query

hypequery generates TypeScript types from your ClickHouse schema. Use count alongside the builder, and reach for raw SQL expressions when the function is not exposed as a dedicated helper.