BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

IN

IN keeps a row if its value is anywhere in a list, like category IN ('Electronics', 'Beauty'). It's a short way to write several ORs on the same column. Text goes in quotes, with commas between.

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

Example

Match any value in a list

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. Keeps a row if the value is anywhere in the list.
    SELECT * FROM products WHERE category IN ('Electronics', 'Beauty', 'Footwear')
  2. Values in brackets, with commas between.
    SELECT * FROM products WHERE category IN ('Electronics', 'Beauty', 'Footwear')
  3. One IN replaces a chain of ORs.
    SELECT * FROM products WHERE category IN ('Electronics', 'Beauty', 'Footwear')

Practice

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

1. Using orders, find the orders in Electronics or Home.
SELECT * FROM orders WHERE category IN ('Electronics', 'Home')
2. Using products, show the products from GlowUp, Lumi and HomeNest.
SELECT * FROM products WHERE brand IN ('GlowUp', 'Lumi', 'HomeNest')
3. Using customers, find the customers in Lagos or Abuja.
SELECT * FROM customers WHERE city IN ('Lagos', 'Abuja')
4. Using store_sales, find the sales from stores ST01 and ST03.
SELECT * FROM store_sales WHERE store_id IN ('ST01', 'ST03')
5. Using shipments, show the shipment_id and carrier of shipments that are pending or in_transit.
SELECT shipment_id, carrier FROM shipments WHERE status IN ('pending', 'in_transit')

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