5.5 KiB
slug | sidebar_position | sidebar_label |
---|---|---|
/en/sql-reference/functions/logical-functions | 110 | Logical |
Logical Functions
Below functions perform logical operations on arguments of arbitrary numeric types. They return either 0 or 1 as UInt8 or in some cases NULL
.
Zero as an argument is considered false
, non-zero values are considered true
.
and
Calculates the logical conjunction of two or more values.
Setting short_circuit_function_evaluation controls whether short-circuit evaluation is used. If enabled, val_i
is evaluated only if (val_1 AND val_2 AND ... AND val_{i-1})
is true
. For example, with short-circuit evaluation, no division-by-zero exception is thrown when executing the query SELECT and(number = 2, intDiv(1, number)) FROM numbers(5)
.
Syntax
and(val1, val2...)
Alias: The AND operator.
Arguments
Returned value
0
, if at least one argument evaluates tofalse
. UInt8 or Nullable(UInt8).NULL
, if no argument evaluates tofalse
and at least one argument isNULL
. NULL.1
, otherwise. UInt8 or Nullable(UInt8).
Example
SELECT and(0, 1, -2);
Result:
┌─and(0, 1, -2)─┐
│ 0 │
└───────────────┘
With NULL
:
SELECT and(NULL, 1, 10, -2);
Result:
┌─and(NULL, 1, 10, -2)─┐
│ ᴺᵁᴸᴸ │
└──────────────────────┘
or
Calculates the logical disjunction of two or more values.
Setting short_circuit_function_evaluation controls whether short-circuit evaluation is used. If enabled, val_i
is evaluated only if ((NOT val_1) AND (NOT val_2) AND ... AND (NOT val_{i-1}))
is true
. For example, with short-circuit evaluation, no division-by-zero exception is thrown when executing the query SELECT or(number = 0, intDiv(1, number) != 0) FROM numbers(5)
.
Syntax
or(val1, val2...)
Alias: The OR operator.
Arguments
Returned value
1
, if at least one argument evaluates totrue
,0
, if all arguments evaluate tofalse
,NULL
, if all arguments evaluate tofalse
and at least one argument isNULL
.
Type: UInt8 or Nullable(UInt8).
Example
SELECT or(1, 0, 0, 2, NULL);
Result:
┌─or(1, 0, 0, 2, NULL)─┐
│ 1 │
└──────────────────────┘
With NULL
:
SELECT or(0, NULL);
Result:
┌─or(0, NULL)─┐
│ ᴺᵁᴸᴸ │
└─────────────┘
not
Calculates the logical negation of a value.
Syntax
not(val);
Alias: The Negation operator.
Arguments
Returned value
1
, ifval
evaluates tofalse
. UInt8 or Nullable(UInt8).0
, ifval
evaluates totrue
. UInt8 or Nullable(UInt8).NULL
, ifval
isNULL
. NULL.
Example
SELECT NOT(1);
Result:
┌─not(1)─┐
│ 0 │
└────────┘
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 etc.
Syntax
xor(val1, val2...)
Arguments
Returned value
1
, for two values: if one of the values evaluates tofalse
and other does not. UInt8 or Nullable(UInt8).0
, for two values: if both values evaluate tofalse
or to bothtrue
. UInt8 or Nullable(UInt8).NULL
, if at least one of the inputs isNULL
. NULL.
Example
SELECT xor(0, 1, 1);
Result:
┌─xor(0, 1, 1)─┐
│ 0 │
└──────────────┘