skip to main content
Reference : Supported SQL Statements and Extensions : SQL Expressions : Operators : Operator Precedence
  

Try DataDirect Drivers Now
Operator Precedence
As expressions become more complex, the order in which the expressions are evaluated becomes important. The following table shows the order in which the operators are evaluated. The operators in the first line are evaluated first, then those in the second line, and so on. Operators in the same line are evaluated left to right in the expression.You can change the order of precedence by using parentheses. Enclosing expressions in parentheses forces them to be evaluated together.
Table 30. Operator Precedence
Precedence
Operator
1
+ (Positive), - (Negative)
2
*(Multiply), / (Division)
3
+ (Add), - (Subtract)
4
|| (Concatenate)
5
=, >, <, >=, <=, <>, != (Comparison operators)
6
NOT, IN, LIKE
7
AND
8
OR

Example A

The query in the following example returns employee records for which the department number is 1 or 2 and the salary is greater than $1000:
SELECT * FROM emp WHERE (deptno = 1 OR deptno = 2) AND sal > 1000
Because parenthetical expressions are forced to be evaluated first, the OR operation takes precedence over AND.

Example B

In the following example, the query returns records for all the employees in department 1, but only employees whose salary is greater than $1000 in department 2.
SELECT * FROM emp WHERE deptno = 1 OR deptno = 2 AND sal > 1000
The AND operator takes precedence over OR, so that the search condition in the example is equivalent to the expression deptno = 1 OR (deptno = 2 AND sal > 1000).