Operators
An SQL operator is a symbol specifying an action that is performed on one or more expressions. Operators are represented by special characters or by keywords.
Operator Precedence
When a complex expression has multiple operators, operator precedence determines the sequence of operations in the expression,
e.g. in expression 1 + 2 * 3
, *
has higher precedence than +
, so the expression is evaluated as 1 + (2 * 3) = 7
.
The order of execution can significantly affect the resulting value.
Operators have the precedence levels shown in the following table. An operator on higher precedence is evaluated before an operator on a lower level. In the following table, the operators in descending order of precedence, a.k.a. 1 is the highest level. Operators listed on the same table cell have the same precedence and are evaluated from left to right or right to left based on the associativity.
Precedence | Operator | Operation | Associativity |
---|---|---|---|
1 | . [] :: |
member access element access cast |
Left to right |
2 | + - ~ |
unary plus unary minus bitwise NOT |
Right to left |
3 | * / % DIV |
multiplication division, modulo integral division |
Left to right |
4 | + - || |
addition subtraction concatenation |
Left to right |
5 | << >> >>> |
bitwise shift left bitwise shift right bitwise shift right unsigned |
Left to right |
6 | & | bitwise AND | Left to right |
7 | ^ | bitwise XOR(exclusive or) | Left to right |
8 | | | bitwise OR(inclusive or) | Left to right |
9 | =, == <>, != <, <= >, >= |
comparison operators | Left to right |
10 | NOT, ! EXISTS |
logical NOT existence |
Right to left |
11 | BETWEEN IN RLIKE, REGEXP ILIKE LIKE IS [NULL, TRUE, FALSE] IS DISTINCT FROM |
other predicates | Left to right |
12 | AND | conjunction | Left to right |
13 | OR | disjunction | Left to right |