skip to main content
SQL Statements for Flat-File Drivers : Select Statement : SQL Expressions : Operator Precedence
  
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.
Table 40. Operator Precedence
Precedence
Operator
1
Unary -, Unary +
2
**
3
*, /
4
+, -
5
=, <>, <, <=, >, >=, LIKE, NOT LIKE, IS NULL, IS NOT NULL, BETWEEN, IN, EXISTS, ANY, ALL
6
NOT
7
AND
8
OR
The following example shows the importance of precedence:
WHERE salary > 40000 OR
hire_date > {01/30/1989} AND
dept = 'D101'
Because AND is evaluated first, this query retrieves employees in department D101 hired after January 30, 1989, as well as every employee making more than $40,000, no matter what department or hire date.
To force the clause to be evaluated in a different order, use parentheses to enclose the conditions to be evaluated first. For example:
WHERE (salary > 40000 OR hire_date > {01/30/1989})
AND dept = 'D101'
retrieves employees in department D101 that either make more than $40,000 or were hired after January 30, 1989.