Multiple Conditions
How to filter rows using AND, OR, and parentheses
Combining conditions with AND
AND requires all conditions to be true for a row to be returned. If any one condition is false, the row is excluded.
-- Only Large Lattes costing $5 or less SELECT * FROM coffee_shop_sales WHERE drink_type = 'Latte' AND size = 'Large' AND price_usd <= 5;
You can chain as many AND conditions as you need. Each one narrows the results further.
Combining conditions with OR
OR requires at least one condition to be true. A row is returned if it satisfies any of the conditions.
-- Any sale that is either a Latte OR costs more than $6 SELECT * FROM coffee_shop_sales WHERE drink_type = 'Latte' OR price_usd > 6;
OR widens the results — you typically get more rows with OR than you would with AND.
Operator precedence and parentheses
This is the most important concept on this page. AND binds more tightly than OR — just like multiplication before addition in maths. Without parentheses, AND is evaluated first, which can produce results you didn't intend.
-- WRONG: AND binds first, so this reads as:
-- "Fruit" OR ("Vegetable" AND quantity > 10)
WHERE category = 'Fruit'
OR category = 'Vegetable'
AND quantity > 10;
-- CORRECT: parentheses force the OR to be evaluated first
WHERE (category = 'Fruit' OR category = 'Vegetable')
AND quantity > 10;
Whenever you mix AND and OR, use parentheses to make the intended grouping explicit — even when it's technically not required.
💡 AND vs OR at a glance
| Operator | Row is returned when… | Effect on results |
|---|---|---|
AND | All conditions are true | Narrows results (fewer rows) |
OR | At least one condition is true | Widens results (more rows) |
BETWEEN — a shorthand for ranges
BETWEEN x AND y is a clean way to filter a column to an inclusive range. It's equivalent to >= x AND <= y.
-- Workouts lasting between 30 and 90 minutes (inclusive) SELECT * FROM gym_member_visits WHERE workout_duration_minutes BETWEEN 30 AND 90; -- Same result using >= and <= WHERE workout_duration_minutes >= 30 AND workout_duration_minutes <= 90;
IN — matching multiple values
IN is a shorthand for multiple OR conditions on the same column. Instead of writing category = 'Fruit' OR category = 'Vegetable' OR category = 'Dairy', you can write:
SELECT *
FROM grocery_store_sales
WHERE category IN ('Fruit', 'Vegetable', 'Dairy');
IN keeps queries shorter and easier to read when you're matching several values for the same column.
Common mistakes
Forgetting parentheses when mixing AND and OR — this is the most common source of subtle bugs in SQL. Always add parentheses around OR groups when combining with AND.
Writing column = 'A' OR 'B' — this is invalid. Each condition must be a complete comparison: column = 'A' OR column = 'B'. The shorthand only works with IN.
BETWEEN is inclusive on both ends — BETWEEN 30 AND 90 includes rows where the value is exactly 30 or exactly 90. If you want to exclude the endpoints, use > 30 AND < 90 instead.
Practice: Multiple Conditions
Apply what you learned above. Write a query that matches each task.
Table Schema
Task
Tip: Ctrl + Enter to submit