Difficulty: Beginner
Estimated time: 10–15 minutes
SQL concepts: WHERE, AND, OR, logical precedence
Goal: Filter rows using multiple conditions in a single query.
The Scenario
A product manager asks:
“Show me Accessories that cost less than 50, or any product that costs more than 200.”
This is a classic case where one condition isn’t enough.
You’ll combine conditions using AND and OR—and learn why parentheses matter.
Database Table
products
| column_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| category | TEXT | Product category |
| price | DECIMAL | Product price |
Sample Data
| product_id | name | category | price |
|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 |
| 102 | Mechanical Keyboard | Accessories | 89.00 |
| 103 | 27-inch Monitor | Displays | 229.99 |
| 104 | USB-C Hub | Accessories | 34.50 |
| 105 | Laptop Stand | Workspace | 39.99 |
| 106 | Webcam | Accessories | 59.99 |
Your Task
Write a SQL query that returns:
namecategoryprice
…but only rows that meet one of these rules:
- Category is Accessories AND price is less than 50
OR - Price is greater than 200
Expected Output
| name | category | price |
|---|---|---|
| Wireless Mouse | Accessories | 24.99 |
| USB-C Hub | Accessories | 34.50 |
| 27-inch Monitor | Displays | 229.99 |
Constraints
- Use both
ANDandOR - Use parentheses to control logic
- Do not use
SELECT *
Hint (Optional)
SQL evaluates conditions in this order:
ANDOR
To make your intent clear, use parentheses.
SELECT
name,
category,
price
FROM products
WHERE (category = 'Accessories' AND price < 50) OR price > 200;
Explanation
(category = 'Accessories' AND price < 50)is evaluated together.OR price > 200adds high-priced items from any category.- Parentheses ensure the logic matches the business rule.
Common Mistakes
- Forgetting parentheses
WHERE category = 'Accessories' AND price < 50 OR price > 200This may work, but it’s unclear and error-prone. - Assuming left-to-right evaluation
- Over-filtering by stacking
ANDs
Optional Extension (Mini Bonus)
Try:
- Return Accessories under 60 OR Displays over 200
- Add a third condition using
AND - Rewrite the logic using multiple
WHEREclauses (and see why it fails)
Next Challenge
Beginner Challenge #18: Sorting Results with ORDER BY
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.