Logical and Bitwise Operators
Logical NOT (!)
Function: Performs a logical NOT operation on an expression. If the expression is true, returns false; if the expression is false, returns true.
Parameters:
expr: Boolean type expression.
Return Result:
- Boolean type value.
Example:
Bitwise NOT (~)
Function: Performs a bitwise NOT operation on the expression, turning every 0 into 1 and every 1 into 0.
Parameters:
expr: Integer type expression.
Return Result:
- A value of the same type as the input expression.
Example:
Not Equal (!=)
Function: Compares two expressions and returns true if they are not equal.
Parameters:
expr1: Boolean type expression.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Modulus (%)
Function: Returns the remainder of dividing two numeric expressions.
Parameters:
expr1: Numeric type expression, including float, double, decimal, tinyint, smallint, int, bigint.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input parameters.
Example:
Multiplication (*)
Function: Returns the product of two numeric expressions.
Parameters:
expr1: Numeric type expression, including float, double, decimal, tinyint, smallint, int, bigint.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input parameters.
Example:
Addition (+)
Function: Returns the sum of two numeric expressions.
Parameters:
expr1: Numeric type expression, including float, double, decimal, tinyint, smallint, int, bigint.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input parameters.
Example:
Positive Sign (+)
Function: Returns the value of the expression without making any changes.
Parameters:
expr: Numeric type expression.
Return Result:
- A value of the same type as the input parameter.
Example:
Subtraction (-)
Function: Returns the difference between two numeric expressions.
Parameters:
expr1: Numeric type expression, including float, double, decimal, tinyint, smallint, int, bigint.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input parameters.
Example:
Negative Sign (-)
Function: Returns the negative value of the expression.
Parameters:
expr: Numeric type expression.
Return Result:
- A value of the same type as the input parameter.
Example:
Division (/)
Function: Returns the quotient of dividing two numeric expressions.
Parameters:
expr1: Numeric type expression, including float, double, decimal.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input parameters.
Example:
Integer Division (DIV)
Function: Returns the integer part of dividing two numeric expressions.
Parameters:
dividend: The dividend, a numeric type expression.divisor: The divisor, an expression of the same type as the dividend.
Return Result:
- Integer type value.
Example:
Less Than (<)
Function: Compares two expressions and returns true if the first expression is less than the second.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Less Than or Equal (<=)
Function: Compares two expressions and returns true if the first expression is less than or equal to the second.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Greater Than (>)
Function: Compares two expressions and returns true if the first expression is greater than the second.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Greater Than or Equal (>=)
Function: Compares two expressions and returns true if the first expression is greater than or equal to the second.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Equal (=)
Function: Compares two expressions and returns true if they are equal.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Equal (==)
Function: Compares two expressions and returns true if they are equal. Functionally identical to =.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Not Equal (<>)
Function: Compares two expressions and returns true if they are not equal. Functionally identical to !=.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
NULL-safe Equal (<=>)
Function: Compares two expressions. Similar to =, but with special handling for NULL values. If both values are NULL, returns true; if only one is NULL, returns false. Equivalent to the SQL standard IS NOT DISTINCT FROM.
Parameters:
expr1: Expression of a comparable type.expr2: Expression of the same type asexpr1.
Return Result:
- Boolean type value.
Example:
Logical AND (AND)
Function: Performs a logical AND operation on two boolean expressions. Returns true only if both expressions are true.
Parameters:
expr1: Boolean type expression.expr2: Boolean type expression.
Return Result:
- Boolean type value.
Example:
Logical OR (OR)
Function: Performs a logical OR operation on two boolean expressions. Returns true if at least one expression is true.
Parameters:
expr1: Boolean type expression.expr2: Boolean type expression.
Return Result:
- Boolean type value.
Example:
Logical NOT (NOT)
Function: Performs a logical NOT operation on a boolean expression. Functionally identical to the ! operator.
Parameters:
expr: Boolean type expression.
Return Result:
- Boolean type value.
Example:
Bitwise AND (&)
Function: Performs a bitwise AND operation on two integer expressions.
Parameters:
expr1: Integer type expression.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input expressions.
Example:
Bitwise OR (|)
Function: Performs a bitwise OR operation on two integer expressions.
Parameters:
expr1: Integer type expression.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input expressions.
Example:
Bitwise XOR ()
Function: Performs a bitwise XOR operation on two integer expressions.
Parameters:
expr1: Integer type expression.expr2: Expression of the same type asexpr1.
Return Result:
- A value of the same type as the input expressions.
Example:
String Concatenation (||)
Function: Concatenates two string expressions.
Parameters:
expr1: String type expression.expr2: String type expression.
Return Result:
- String type value.
Example:
Type Cast (::)
Function: Converts an expression to a specified data type. This is PostgreSQL-style type cast syntax, equivalent to CAST(expr AS type). Chained casts are supported.
Syntax:
Example:
IS [NOT] DISTINCT FROM
Function: NULL-safe equality comparison. Unlike =, returns true when both values are NULL (instead of NULL), and returns false when one value is NULL and the other is not (instead of NULL). IS NOT DISTINCT FROM is equivalent to <=>.
Syntax:
Return Result:
- Boolean type value, never returns NULL.
Example:
BETWEEN ... AND
Function: Determines whether the value of an expression falls within a specified range (inclusive of boundaries).
Syntax:
Return Result:
- Boolean type value.
expr BETWEEN lower AND upperis equivalent toexpr >= lower AND expr <= upper.
Example:
LIKE ANY / LIKE SOME / LIKE ALL
Function: Matches an expression against multiple patterns using LIKE. LIKE ANY (or LIKE SOME) returns true if any pattern matches; LIKE ALL returns true only if all patterns match.
Syntax:
Return Result:
- Boolean type value.
Example:
ILIKE
Function: Case-insensitive LIKE matching. Usage is identical to LIKE, but matching ignores case. Also supports the ESCAPE clause and ANY/ALL forms.
Syntax:
Example:
IS [NOT] TRUE / FALSE / UNKNOWN
Function: Determines whether the value of a boolean expression is TRUE, FALSE, or UNKNOWN (NULL). Unlike direct comparison, these predicates do not return NULL due to NULL input.
Syntax:
Return Result:
- Boolean type value, never returns NULL.
Example:
RLIKE / REGEXP
Function: Matches a string using a regular expression. RLIKE and REGEXP are synonyms.
Syntax:
Return Result:
- Boolean type value. Returns true if expr matches the regular expression pattern.
Example:
Operator Precedence
The following table lists the precedence of operators and predicates, from highest to lowest. Operators on the same row have the same precedence and are all left-associative (unless otherwise noted).
| Precedence | Operator / Predicate | Associativity | Description |
|---|---|---|---|
| 1 | . | Left | Field access (table.column, struct.field) |
| 2 | :: | Left | Type cast |
| 3 | [] | Left | Array/Map subscript |
| 4 | + - ~ (unary) | Right | Positive sign, negative sign, bitwise NOT |
| 5 | * / % DIV | Left | Multiplication, division, modulus, integer division |
| 6 | + - || | Left | Addition, subtraction, string concatenation |
| 7 | & | Left | Bitwise AND |
| 8 | `` | Left | Bitwise XOR |
| 9 | | | Left | Bitwise OR |
| 10 | = <=> != <> < <= > >= | Left | Comparison operators |
| 11 | BETWEEN IN LIKE ILIKE RLIKE REGEXP | — | Range, set, pattern matching |
| 12 | IS NULL IS TRUE IS FALSE IS DISTINCT FROM | — | IS predicates |
| 13 | NOT | Right | Logical NOT |
| 14 | AND | Left | Logical AND |
| 15 | OR | Left | Logical OR |
Notes:
- The lower the precedence number, the higher the precedence.
- Parentheses
()can be used to change the order of operations. NOTcan serve as a prefix modifier for predicates such asBETWEEN,IN,LIKE(e.g.,NOT BETWEEN); in this case, it is part of the predicate and is not affected by the precedence of logical NOT.
