Looking for Values in a List: The IN Operator

Earlier, when you were looking at multiple comparisons with AND and OR, you ran a query to find products whose product names matched one of several values. This is the query:

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

The IN operator allows you to compress the four conditions into a single condition like this:

select ProductName, UnitPrice
  from Products
 where ProductName in
             ('Konbu', 'Cream Cheese', 'Tofu', 'Pavlova')

Both forms of the query return the same results, and they are identically processed by the server. The second form with IN is more compact and easier to read.

In combination ...

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.