Total Pageviews

Logical Operator Precedence in SQL Server

  • Just like operator precedence in Mathematics, in SQL Server for LOGICAL OPERATOR, we have the precedence.
  • When we have multiple operators in our expression, operator precedence determines the sequence in which the operations are performed. The order of execution can significantly affect the resulting value.
  • With the help of parentheses, we can control the operator precedence. Everything within the parentheses is evaluated first to yield a single value before that value can be used by any operator outside the parentheses.
  • If an expression has nested parentheses, the most deeply nested expression is evaluated first.
  • When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression.
  • When more than one logical operator is used in a statement, NOT is evaluated first, then AND, and finally OR. Arithmetic, and bitwise, operators are handled before logical operators.
Logical operator precedence more specifically per the below table:-
 

EXECUTION Level
Operators
1
~ (Bitwise NOT)
2
* (Multiply), / (Division), % (Modulo)
3
+ (Positive), - (Negative), + (Add), (+ Concatenate), - (Subtract), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)
4
=, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)
5
NOT
6
AND
7
ALL, ANY, BETWEEN, IN, LIKE, OR, SOME
8
= (Assignment)
 

No comments:

Post a Comment