How to read the select statement to understand the result [closed]
-
12-03-2021 - |
Question
I am convinced that it is a very basic question, but I still have to ask it, making queries on the Oracle HR schema I notice that the result of these two queries is different:
I would like to know why this happens, when using the parentheses it gives me the result I want, while if I do not use the parentheses it gives me another record (Pat Fay) that does not make sense, how should I read this sentence to understand these outputs, thank you very much .
Solution
it's about order of operations: "and" happens before "or", so
where department_id=20 or department_id=50 and salary >= 8000
is actually the same as
where department_id=20 or ( department_id=50 and salary >= 8000 )
and has a different result than
where ( department_id=20 or department_id=50 ) and salary >= 8000
where the parenthesis force the order of operations to be different and the department ids are evaluated together.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange