Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of operations for SQL?

Suppose I have this statement:

SELECT * FROM MyTable WHERE a = 1 or b = 2 and c = 3

Does that mean: (a = 1) OR (b = 2 AND c = 3) or does it mean (a = 1 or b = 2) AND c = 3? Can I change what it means, i.e. execute the OR before the AND or is this not possible?

like image 933
meds Avatar asked Dec 27 '22 21:12

meds


1 Answers

From Technet:

When more than one logical operator is used in a statement, AND operators are evaluated first. You can change the order of evaluation by using parentheses.

So yes, it means (a = 1) OR (b = 2 AND c = 3).

You can force the behavior you want by writing the parentheses as you did above: (a = 1 OR b = 2) AND c = 3

like image 99
Arithmomaniac Avatar answered Jan 17 '23 21:01

Arithmomaniac