Friday, 14 November 2014

SQL Operator Precedence

Just a small note for me to remember how this works.

select * 
from ((select 'FINANCE' as dept,  42 as employees from dual) union 
      (select 'SALES' as dept, 32 as employees from dual)) departments
where
   departments.dept = 'FINANCE'
or
   departments.dept = 'SALES'
and
   departments.employees < 30;
returns: FINANCE 42

Clearly, AND takes precedence over OR, as usual.
AND B OR C => (A AND B) OR C

OR B AND C => A OR (B AND C)

References

Stackoverflow - sql logic operator precedence
http://stackoverflow.com/questions/1241142/sql-logic-operator-precedence-and-and-or
SQL Logical Operators
http://www.praetoriate.com/t_garmany_easysql_sql_logical_operators.htm

1 comment: