SolveWithSQL

SQL Beginner Challenge 17: Combining Conditions with AND and OR

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_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price

Sample Data

product_idnamecategoryprice
101Wireless MouseAccessories24.99
102Mechanical KeyboardAccessories89.00
10327-inch MonitorDisplays229.99
104USB-C HubAccessories34.50
105Laptop StandWorkspace39.99
106WebcamAccessories59.99

Your Task

Write a SQL query that returns:

  • name
  • category
  • price

…but only rows that meet one of these rules:

  1. Category is Accessories AND price is less than 50
    OR
  2. Price is greater than 200

Expected Output

namecategoryprice
Wireless MouseAccessories24.99
USB-C HubAccessories34.50
27-inch MonitorDisplays229.99

Constraints

  • Use both AND and OR
  • Use parentheses to control logic
  • Do not use SELECT *

Hint (Optional)

SQL evaluates conditions in this order:

  1. AND
  2. OR

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 > 200 adds high-priced items from any category.
  • Parentheses ensure the logic matches the business rule.

Common Mistakes

  1. Forgetting parentheses
    WHERE category = 'Accessories' AND price < 50 OR price > 200 This may work, but it’s unclear and error-prone.
  2. Assuming left-to-right evaluation
  3. Over-filtering by stacking ANDs

Optional Extension (Mini Bonus)

Try:

  1. Return Accessories under 60 OR Displays over 200
  2. Add a third condition using AND
  3. Rewrite the logic using multiple WHERE clauses (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.