Wednesday 29 May 2013

Operator Precedence in SQL


  • The rules of precedence determines the order in which expressions are evaluated and calculated.
  • The table below lists the default order of precedence.
  • You can override the default order by using parentheses around the expressions you want to calculate first.

   ORDER EVALUATED                     OPERATOR

                   1                                        Arithmetic Operator
                   2                                        Concatenation Operator
                   3                                        Comparison Conditions
                   4                                        IS [NOT] NULL, LIKE, [NOT] IN
                   5                                        [NOT] BETWEEN
                   6                                        NOT logical condition
                   7                                        AND logical condition
                   8                                        OR logical condition

NOTE:

Override rules of precedence by using parentheses.

Example:  (Carefully study both the scenario in the example)


1) In the first scenario first AND operator is calculated and then the result is operated with OR.
2) In the second scenario first OR operator is calculated because of Parentheses and then result is operated with AND.


No comments:

Post a Comment