Lessons › SQL › Chapter 2: Filtering
SQL lesson
Combining WHERE with AND and OR
AND keeps a row only if both rules are true. OR keeps it if either rule is true. When you mix them, put brackets around the part that goes together, so SQL reads it the way you mean.
Try this lesson free →Example
Two rules at once
SELECT *
FROM orders
WHERE category = 'Electronics' AND revenue > 200| order_id | customer_id | category | revenue | order_date |
|---|---|---|---|---|
| O2001 | C101 | Electronics | 299.99 | 2024-03-01 |
Step by step
- Keeps a row only if both rules are true.
SELECT * FROM orders WHERE (category = 'Electronics' OR category = 'Clothing') AND status = 'shipped' - Keeps a row if at least one rule is true.
SELECT * FROM orders WHERE (category = 'Electronics' OR category = 'Clothing') AND status = 'shipped' - Brackets are worked out first, like in maths.
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 Electronics products that cost more than 200.
SELECT * FROM products WHERE category = 'Electronics' AND price > 200Learn 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 →