BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

IN

IN checks whether a value matches anything in a list you provide, collapsing a chain of OR conditions into a single, readable filter.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Filtering Retail Products with IN

SELECT * FROM products
WHERE category IN ('Electronics', 'Beauty', 'Footwear')

Result:

product_idproduct_namebrandcategoryprice
P2001Wireless MouseNovaTechElectronics29.99
P2003Face CleanserGlowUpBeauty18.5
P2004Running ShoesFitGearFootwear119.99
P2006Lip Balm SetGlowUpBeauty14.99

Step by step

  1. The asterisk symbol means all columns.
    SELECT *
  2. FROM tells SQL which table to pull the data from.
    SELECT *
    FROM products
  3. WHERE keeps only the rows that match your condition.
    SELECT *
    FROM products
    WHERE category IN ('Electronics', 'Beauty', 'Footwear');

Practice

Try each one in your head first, then open the answer.

1. Using orders, write a query to find rows where customer_id is one of: 'C101', 'C102'.
SELECT * FROM orders WHERE customer_id IN ('C101', 'C102')
2. Using products, write a query to find rows where product_id is one of: 'P2001', 'P2002'.
SELECT * FROM products WHERE product_id IN ('P2001', 'P2002')
3. Using customers, write a query to find rows where email is one of: 'alice.chen@shopmail.com', 'bob.tanaka@retail.net'.
SELECT * FROM customers WHERE email IN ('alice.chen@shopmail.com', 'bob.tanaka@retail.net')
4. Using store_sales, write a query to find rows where sale_id is one of: 'S9001', 'S9002'.
SELECT * FROM store_sales WHERE sale_id IN ('S9001', 'S9002')
5. Using returns, write a query to find rows where return_id is one of: 'R5001', 'R5002'.
SELECT * FROM returns WHERE return_id IN ('R5001', 'R5002')

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed