ClickHouse Functions/Math Functions

intDiv

Integer division — bucket IDs, page numbers, and fixed-width range grouping.

Signature

intDiv(a: Integer, b: Integer): Integer

Returns

Integer (same width as input)

What it does

Performs integer division, discarding the remainder (equivalent to floor(a/b) for positive numbers). Used for fixed-width range bucketing and histogram construction.

intDiv() is the idiomatic way to create fixed-width numeric buckets in ClickHouse. intDiv(price, 10) * 10 maps any price to the start of its $10 range. intDiv(user_id, 1000) shards users into groups of 1000. Unlike the / operator which returns Float64 for Integer inputs, intDiv returns an Integer — making it safe for GROUP BY and PARTITION BY.

Notes

  • intDiv throws on division by zero — use intDivOrZero() for null-safe behaviour.
  • For Float inputs, use floor(a / b) instead.
  • intDiv(id, N) * N gives the lower bound of each N-wide bucket.

Example SQL

intDiv in ClickHouse SQL

intDiv-example.sql

TypeScript with hypequery

Use intDiv 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 intDiv when you need them inside a builder query.

price-histogram.ts

Common questions

What developers search for with intDiv

ClickHouse histogram bucketing

intDiv(col, bucket_size) * bucket_size maps each value to the lower bound of its bucket. GROUP BY on that expression gives histogram bins.

FAQ

Frequently asked questions about intDiv

What happens if I divide by zero with intDiv?

intDiv throws an exception on division by zero. Use intDivOrZero(a, b) which returns 0 instead of throwing.

Related functions

Functions used alongside intDiv

Next step

Use intDiv in a type-safe TypeScript query

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