Expressions in where clause process in the following order:
Number | Expression |
1 | Arithmetic Operators |
2 | Concatenation Operator |
3 | Comparision Condition |
4 | IS [NOT] NULL, LIKE, [NOT] IN |
5 | [NOT] BETWEEN |
6 | Not Equal To |
7 | NOT Logical Condition |
8 | AND Logical Condition |
9 | OR Logical Condition |
For example on AND and OR conditions:
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0 Connected as hr SQL> set serveroutput on SQL> SQL> SELECT t.employee_id, t.first_name, t.last_name, t.salary ,t.manager_id 2 FROM employees t 3 WHERE t.salary > 15000 4 AND t.manager_id = 100 5 OR t.manager_id = 103; EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY MANAGER_ID ----------- -------------------- ------------------------- ---------- ---------- 101 Neena Kochhar 17000,00 100 102 Lex De Haan 17000,00 100 104 Bruce Ernst 6000,00 103 105 David Austin 4800,00 103 106 Valli Pataballa 4800,00 103 107 Diana Lorentz 4200,00 103 6 rows selected SQL> SELECT t.employee_id, t.first_name, t.last_name, t.salary ,t.manager_id 2 FROM employees t 3 WHERE t.salary > 15000 4 OR t.manager_id = 100 5 AND t.manager_id = 103; EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY MANAGER_ID ----------- -------------------- ------------------------- ---------- ---------- 210 Sue Andreas 20000,00 145 100 Steven Black 24000,00 101 Neena Kochhar 17000,00 100 102 Lex De Haan 17000,00 100 SQL>
In the first query, we try to find employees supply this condition => (salary>15000 and manager_id = 100) or (manager_id=103).
In the second query, we try to find employees supply this condition => (manager_id = 100 and manager_id = 103) or (salary > 15000)
It amazes me how often I see posts like this outlining the order in which conditions were evaluated. Not because the topic isn’t a little confusing, but because it is so very unnecessary.
The solution I always use is (), and I can’t understand why you would ever NOT use them, especially in a clause that can be confusing in any way.
If I write some SQL like you have, perhaps I know how these things will evaluate and I can tell what the results will be.
However, I can’t guarantee that the next guy to look at this code will understand it.
So, in your example, I would ALWAYS do
(manager_id=100 and manager_id=103) or (salary > 15000) because that is ALWAYS clear and easy to understand. I can count on the next guy knowing what the query does. Heck, I can count on even a non-sql person understanding it.
It’s usefull, in the case that you find a stored procedure which contents similar query, and you try to figure out, what was the reason because the previous database administrator wrote that. What was his logic?
really good explanation.. thanks..
@moleboy,
what if you ARE that next guy who looks at the code? i needed it explained to me because in my case, the actual AND/OR keyword is a variable in multiple positions in the query…
Don’t you mean…
WHERE (manager_id=100 OR manager_id=103) AND (salary > 15000)
It’s impossible to have (manager_id=100 AND manager_id=103), so the selection of records returned will always be (salary > 15000).
Yes, my friend