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 →Example
Match any value in a list
SELECT *
FROM products
WHERE category IN ('Electronics', 'Beauty', 'Footwear')| product_id | product_name | brand | category | price |
|---|---|---|---|---|
| P2001 | Wireless Mouse | NovaTech | Electronics | 29.99 |
| P2003 | Face Cleanser | GlowUp | Beauty | 18.5 |
| P2004 | Running Shoes | FitGear | Footwear | 119.99 |
| P2006 | Lip Balm Set | GlowUp | Beauty | 14.99 |
Step by step
- Keeps a row if the value is anywhere in the list.
SELECT * FROM products WHERE category IN ('Electronics', 'Beauty', 'Footwear') - Values in brackets, with commas between.
SELECT * FROM products WHERE category IN ('Electronics', 'Beauty', 'Footwear') - One IN replaces a chain of ORs.
SELECT * FROM products WHERE category IN ('Electronics', 'Beauty', 'Footwear')
Practice
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 →