Combining Conditions with OR and AND

You will often need to combine conditions in the WHERE clause to find rows that meet multiple criteria. For example, you might want to see the price of several products, as in this query:

select ProductName, UnitPrice
  from Products
 where ProductName = 'Konbu'
    or ProductName = 'Cream Cheese'
    or ProductName = 'Tofu'
    or ProductName = 'Pavlova'

Results:

ProductName                              UnitPrice
---------------------------------------- ---------------------
Konbu                                                   6.0000
Pavlova                                                17.4500
Tofu                                                   23.2500

The four comparisons in the query are joined with the keyword OR. As in all programming environments, when you use OR, the condition is true when any of the joined conditions is true. For each row, the server tests each of the conditions. ...

Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.