Lessons › SQL › Chapter 2: Filtering
SQL lesson
Combining WHERE with AND and OR
Combining AND and OR lets you stack multiple conditions onto a single filter - AND means every condition must be true to keep a row, while OR means satisfying just one is enough.
Try this lesson free →Example
Filtering Retail Orders with WHERE, AND, and OR
SELECT * FROM orders
WHERE (category = 'Electronics' OR category = 'Clothing')
AND status = 'shipped'| order_id | category | status | revenue | order_date |
|---|---|---|---|---|
| O2001 | Electronics | shipped | 299.99 | 2024-03-01 |
| O2005 | Clothing | shipped | 62.0 | 2024-03-05 |
Step by step
- The asterisk symbol means all columns.
SELECT * - FROM tells SQL which table to pull the data from.
SELECT * FROM orders - WHERE keeps only the rows that match your condition.
SELECT * FROM orders WHERE (category = 'Electronics' OR category = 'Clothing') AND status = 'shipped';
Practice
1. Using orders, find the orders where category is 'Electronics' AND revenue is less than 200.
SELECT * FROM orders WHERE category = 'Electronics' AND revenue < 2002. Using products, find the products where category is 'Beauty' OR category is 'Home'.
SELECT * FROM products WHERE category = 'Beauty' OR category = 'Home'3. Using orders, find the orders where revenue is greater than 250 OR category is 'Clothing'.
SELECT * FROM orders WHERE revenue > 250 OR category = 'Clothing'4. Using orders, find the Clothing orders worth more than 100, plus every order from customer 'C103'.
SELECT * FROM orders WHERE (category = 'Clothing' AND revenue > 100) OR customer_id = 'C103'5. Using products, find the products in 'Electronics' or 'Clothing' that cost less than 100.
SELECT * FROM products WHERE (category = 'Electronics' OR category = 'Clothing') AND price < 100Learn 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 →