Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

127


Chapter 6: Introducing Basic Query Flow


6


Multiple WHERE Conditions
You can combine multiple WHERE conditions within the WHERE clause using the Boolean
logical operators: AND, OR, and NOT. As with the mathematical operators of multiplication
and division, an order of precedence exists with the Boolean logical operators: NOT comes
fi rst, then AND, and then OR:

USE AdventureWorks;
SELECT ProductID, Name
FROM Production.Product
WHERE
Name LIKE 'Chain%'
OR
ProductID BETWEEN 320 AND 324
AND
Name Like '%s%';

Result:

ProductID Name
----------- ---------------------
952 Chain
324 Chain Stays
322 Chainring
320 Chainring Bolts
321 Chainring Nut

With parentheses, the result of the query is radically changed:

USE AdventureWorks;
SELECT ProductID, Name
FROM Production.Product
WHERE
(
Name LIKE 'Chain%'
OR
ProductID BETWEEN 320 AND 324
)
AND
Name Like '%s%';

Result:

ProductID Name
--------------- ---------------------
324 Chain Stays
320 Chainring Bolts

c06.indd 127c06.indd 127 7/30/2012 4:16:03 PM7/30/2012 4:16:03 PM


http://www.it-ebooks.info
Free download pdf