Documentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Conditional functions
Overview
Using Conditional Results Directly
Conditionals always result to0, 1 or NULL. So you can use conditional results directly like this:
NULL Values in Conditionals
WhenNULL values are involved in conditionals, the result will also be NULL.
Nullable.
The following example demonstrates this by failing to add equals condition to multiIf.
CASE statement
The CASE expression in ClickHouse provides conditional logic similar to the SQL CASE operator. It evaluates conditions and returns values based on the first matching condition. ClickHouse supports two forms of CASE:CASE WHEN ... THEN ... ELSE ... END
This form allows full flexibility and is internally implemented using the multiIf function. Each condition is evaluated independently, and expressions can include non-constant values.
CASE <expr> WHEN <val1> THEN ... WHEN <val2> THEN ... ELSE ... END
This more compact form is optimized for constant value matching and internally usescaseWithExpression().
Caveats
ClickHouse determines the result type of a CASE expression (or its internal equivalent, such asmultiIf) before evaluating any conditions. This is important when the return expressions differ in type, such as different timezones or numeric types.
- The result type is selected based on the largest compatible type among all branches.
- Once this type is selected, all other branches are implicitly cast to it - even if their logic would never be executed at runtime.
- For types like DateTime64, where the timezone is part of the type signature, this can lead to surprising behavior: the first encountered timezone may be used for all branches, even when other branches specify different timezones.
Asia/Kolkata
DateTime64(3, <timezone>) return types. It infers the common type as DateTime64(3, 'Asia/Kolkata' as the first one it sees, implicitly casting other branches to this type.
This can be addressed by converting to a string to preserve intended timezone formatting:
clamp
Introduced in: v24.5.0 Restricts a value to be within the specified minimum and maximum bounds. If the value is less than the minimum, returns the minimum. If the value is greater than the maximum, returns the maximum. Otherwise, returns the value itself. All arguments must be of comparable types. The result type is the largest compatible type among all arguments. Syntaxvalue— The value to clamp. -min— The minimum bound. -max— The maximum bound.
Query
Response
Query
Response
Query
Response
greatest
Introduced in: v1.1.0 Returns the greatest value among the arguments.NULL arguments are ignored.
- For arrays, returns the lexicographically greatest array.
- For
DateTimetypes, the result type is promoted to the largest type (e.g.,DateTime64if mixed withDateTime32).
Use setting
least_greatest_legacy_null_behavior to change NULL behaviorVersion 24.12 introduced a backwards-incompatible change such that NULL values are ignored, while previously it returned NULL if one of the arguments was NULL.
To retain the previous behavior, set setting least_greatest_legacy_null_behavior (default: false) to true.x1[, x2, ...]— One or multiple values to compare. All arguments must be of comparable types.Any
Any
Examples
Numeric types
Query
Response
Query
Response
Query
Response
if
Introduced in: v1.1.0 Performs conditional branching.- If the condition
condevaluates to a non-zero value, the function returns the result of the expressionthen. - If
condevaluates to zero or NULL, the result of theelseexpression is returned.
short_circuit_function_evaluation controls whether short-circuit evaluation is used.
If enabled, the then expression is evaluated only on rows where cond is true and the else expression where cond is false.
For example, with short-circuit evaluation, no division-by-zero exception is thrown when executing the following query:
then and else must be of a similar type.
Syntax
cond— The evaluated condition.UInt8orNullable(UInt8)orNULLthen— The expression returned ifcondis true. -else— The expression returned ifcondis false orNULL.
then or else expressions, depending on condition cond.
Examples
Example usage
Query
Response
least
Introduced in: v1.1.0 Returns the smallest value among the arguments.NULL arguments are ignored.
- For arrays, returns the lexicographically least array.
- For DateTime types, the result type is promoted to the largest type (e.g., DateTime64 if mixed with DateTime32).
Use setting
least_greatest_legacy_null_behavior to change NULL behaviorVersion 24.12 introduced a backwards-incompatible change such that NULL values are ignored, while previously it returned NULL if one of the arguments was NULL.
To retain the previous behavior, set setting least_greatest_legacy_null_behavior (default: false) to true.x1[, x2, ...]— A single value or multiple values to compare. All arguments must be of comparable types.Any
Any
Examples
Numeric types
Query
Response
Query
Response
Query
Response
multiIf
Introduced in: v1.1.0 Allows writing theCASE operator more compactly in the query.
Evaluates each condition in order. For the first condition that is true (non-zero and not NULL), returns the corresponding branch value.
If none of the conditions are true, returns the else value.
Setting short_circuit_function_evaluation controls
whether short-circuit evaluation is used. If enabled, the then_i expression is evaluated only on rows where
((NOT cond_1) AND ... AND (NOT cond_{i-1}) AND cond_i) is true.
For example, with short-circuit evaluation, no division-by-zero exception is thrown when executing the following query:
NULL conditions are treated as false.
Syntax
caseWithoutExpression, caseWithoutExpr
Arguments
cond_N— The N-th evaluated condition which controls ifthen_Nis returned.UInt8orNullable(UInt8)orNULLthen_N— The result of the function whencond_Nis true. -else— The result of the function if none of the conditions is true.
then_N for matching cond_N, otherwise returns the else condition.
Examples
Example usage
Query
Response