Logical operators as arithmetic ones are being estimated in accordance with their seniority.
One-place operation NOT has highest priority. We can assure ourselves if we'll execute the two following queries.
Console
-- models which are not PC;
-- the second predicate is useless because it is entering condition that yet taken into account
-- by the first predicate
SELECT maker, model, type
FROM Product
WHERE NOT type='PC' OR type='Printer';
Console
-- models of maker A, which are not PC
SELECT maker, model, type
FROM Product
WHERE NOT type='PC' AND maker='A';
Parentheses can change the order of estimation of logical operators:
Console
-- models that are not PC or printer, i.e. models of laptops in our case
SELECT maker, model, type
FROM Product
WHERE NOT (type='PC' OR type='Printer');
Console
-- models that are not PC produced by maker A
SELECT maker, model, type
FROM Product
WHERE NOT (type='PC' AND maker='A');
Next priority has AND operator. Compare results of the following queries.
Console
-- PC models produced by maker A and models of any type belonging to maker B
SELECT maker, model, type
FROM Product
WHERE type='PC' AND maker='A' OR maker='B';
Console
-- PC models produced by maker A or maker B
SELECT maker, model, type
FROM Product
WHERE type='PC' AND (maker='A' OR maker='B');
Notes:
If you are afraid to forget the order in which logical operators are being estimated, use the parentheses.
Next page