-2

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:

enter image description here

Expected result: enter image description here

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 .

2
  • 2
    Because AND has higher operator precedence than OR. This concept applies generally to almost all languages - precedence matters.
    – SMor
    Dec 18, 2020 at 4:22
  • 1
    This order of precedence doesn't ONLY apply to "almost all languages". It is a basic, fundamental mathematical concept that one learns on the first day of Algebra I. In fact, I'd say that if there is a language that does not implement it ("applies generally to almost all languages) then that language is fundamentally flawed.
    – EdStevens
    Dec 18, 2020 at 15:44

1 Answer 1

4

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.