Logical functions
Perform boolean logic operations.
Logical functions perform boolean operations on one or more values. They are crucial for:
- Conditional processing
- Filtering data
- Implementing complex logical conditions in queries
Key features:
-
Support for various data types:
- Integers
- Floating-point numbers
- Nullable values
-
Flexible syntax options:
- Functional syntax (e.g.,
and(a, b)
) - Operator syntax (e.g.,
a AND b
)
- Functional syntax (e.g.,
These functions enable you to create readable and expressive logical expressions in your queries, enhancing both clarity and functionality.
ClickHouse function reference
Logical functions perform boolean operations on one or more values.
and
Calculates the logical conjunction of two or more values.
Syntax:
Alternatively, you can use the AND
operator:
Alias:
- The
AND
operator
Arguments:
val1
,val2
, … (Int
,UInt
,Float
, orNullable
): List of at least two values.
Returns:
0
if at least one argument evaluates to falseNULL
if no argument evaluates to false and at least one argument is NULL1
otherwise
Return type: UInt8
or Nullable(UInt8)
Example:
In this example, should_celebrate
is true only when it’s Taco Tuesday, there’s guacamole, and more than 3 tacos are available.
Result:
or
Calculates the logical disjunction of two or more values.
Syntax:
Alias:
- The
OR
operator
Arguments:
val1
,val2
, … (Int
,UInt
,Float
, orNullable
): List of at least two values.
Returns:
1
if at least one argument evaluates to true0
if all arguments evaluate to falseNULL
if all arguments evaluate to false and at least one argument is NULL
Return type: UInt8
or Nullable(UInt8)
.
Example:
Result:
In this example, hasProteinOption
is true if at least one protein option (carnitas, chicken, or beef) is available.
not
Calculates the logical negation of a value.
Syntax:
Alias:
- The Negation operator
!
Arguments:
val
(Int
,UInt
,Float
, orNullable
): The value.
Returns:
1
ifval
evaluates to false0
ifval
evaluates to trueNULL
ifval
is NULL
Return type: UInt8
or Nullable(UInt8)
.
Example:
Result:
In this example, isMild
is the logical negation of isSpicy
.
xor
Calculates the logical exclusive disjunction of two or more values. For more than two input values, the function first xor-s the first two values, then xor-s the result with the third value, and so on.
Syntax:
Arguments:
val1
,val2
, … (Int
,UInt
,Float
, orNullable
): List of at least two values.
Returns:
- For two values:
1
if one of the values evaluates to false and the other does not0
if both values evaluate to false or both true
NULL
if at least one of the inputs is NULL
Return type: UInt8
or Nullable(UInt8)
.
Example:
Result:
In this example, hasUniqueTopping
is true if exactly one of salsa, guacamole, or sour cream is present on the taco.
not
Calculates the logical negation of a value.
Syntax:
Alternatively, you can use the negation operator:
Arguments:
val
(Int
,UInt
,Float
, orNullable
): The value to negate.
Returns:
1
ifval
evaluates to false0
ifval
evaluates to trueNULL
ifval
is NULL
Type: UInt8
or Nullable(UInt8)
Example:
Result:
In this example:
not(1)
returns 0 because 1 is considered truenot(0)
returns 1 because 0 is considered falsenot(NULL)
returns NULLnot(isHot('jalapeño'))
returns 0, assuming jalapeños are considered hot
The not
function treats any non-zero value as true and zero as false. This applies to all numeric types, including floating-point numbers.
Was this page helpful?