When more than one logical operator is used in a statement, NOT is evaluated first, then AND, and finally OR. Arithmetic (and bitwise) operators are handled before logical operators.
For example, the following query finds all business books in the titles table, no matter what their advances are, as well as all psychology books that have an advance greater than $5500:
SELECT title_id, type, advance FROM titles WHERE type = 'business' OR type = 'psychology' AND advance > $5500 title_id type advance -------- ------------ -------------------------- BU1032 business 5,000.00 BU1111 business 5,000.00 BU2075 business 10,125.00 BU7832 business 5,000.00 PS1372 psychology 7,000.00 PS2106 psychology 6,000.00 (6 row(s) affected)
In this example, the advance condition pertains to psychology books and not to business books because AND has precedence over OR.
You can change the meaning of the query by adding parentheses to force evaluation of the OR first. This query finds all business and psychology books that have advances over $5500:
SELECT title_id, type, advance FROM titles WHERE (type = 'business' OR type = 'psychology') AND advance > $5500 title_id type advance ------------ -------------------------- ----------- business 6,281.25 30788 mod_cook 7,500.00 24278 popular_comp 7,500.00 12875 psychology 4,255.00 9939 trad_cook 6,333.33 19566 UNDECIDED (null) (null) (6 row(s) affected)
The use of parentheses, even when not required, can improve readability of queries and reduce the chance of making a subtle mistake because of operator precedence. There is no significant performance penalty in using parentheses.
For more information, see Search Conditions in the Microsoft SQL Server Transact-SQL Reference.