M
M
Maxim Golovlev2021-12-19 14:19:40
SQL Server
Maxim Golovlev, 2021-12-19 14:19:40

What is the precedence of operations in WHERE?

Print the title, author, price, and quantity of all books whose price is less than 500 or greater than 600, and the cost of all copies of these books is greater than or equal to 5000.

Request 1:
select title,
       author,
       price,
       amount,
from book
where price > 600 or price < 500 and price * amount >= 5000


Request 2:
select title,
       author,
       price,
       amount,
from book
where (price > 600 or price < 500) and price * amount >= 5000


Query 1 does not produce the desired result. As I understand it, it's all about prioritization after "where", because the second query produced the desired result after I enclosed price > 600 or price < 500 in parentheses.
Please explain so that in the future there will be understandable similar moments.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-12-19
@golovlevarts

https://docs.microsoft.com/ru-ru/sql/t-sql/languag...

B
BorLaze, 2021-12-19
@BorLaze

or - this is addition
and - multiplication
1 + 2 * 3and (1 + 2) * 3- is there a difference?
It should be noted that it is strange somehow - to start understanding integrals, skipping elementary arithmetic ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question