SQL Nested AND/OR Practice Exercises
Learn to group and nest AND/OR conditions and practice with free exercises
Why nesting matters
As a reminder, AND binds more tightly than OR — SQL evaluates every AND pair before it looks at the surrounding ORs. The moment you combine an OR choice with an AND condition, you need parentheses to group the OR so it's evaluated as a single unit first.
-- Fruit or Vegetable sales, where quantity is over 10 SELECT * FROM grocery_store_sales WHERE (category = 'Fruit' OR category = 'Vegetable') AND quantity > 10;
The parentheses group the two category options into one unit. SQL evaluates that group first, then applies AND quantity > 10 to the result — exactly the logic intended.
Combining two OR groups with AND
Nesting isn't limited to one group — you can combine multiple parenthesized OR groups with AND to express more specific logic, such as "one of these categories, and one of these conditions."
-- Electronics or Appliances, where stock is low OR the unit cost is high SELECT * FROM product_inventory WHERE (category = 'Electronics' OR category = 'Appliances') AND (stock_quantity < 20 OR unit_cost > 100);
Each parenthesized group is evaluated on its own first. The outer AND then requires both groups to be true — one category match, and at least one of the two numeric conditions.
💡 Reading nested conditions
| Step | What to do |
|---|---|
| 1. Find the innermost parentheses | Evaluate that group as a single true/false unit first |
| 2. Work outward, group by group | Treat each parenthesized group as one value once it's resolved |
| 3. Apply AND / OR / NOT last | Combine the resolved groups using the operators outside them |
NOT with parentheses
NOT applies to everything inside the parentheses that follow it — not just the first condition. This is a common trip-up: NOT (A OR B) excludes rows matching either A or B, not just A.
-- Anything that is NOT a Standard or Economy room SELECT * FROM hotel_room_bookings WHERE NOT (room_type = 'Standard' OR room_type = 'Economy');
Without the parentheses, NOT room_type = 'Standard' OR room_type = 'Economy' would mean something different — the NOT would apply only to the first comparison, not the second.
Grouping three or more values
An OR group can hold more than two options. Once a group has three or more equality checks on the same column, it's often cleaner to rewrite it with IN — but the nested OR form works identically.
-- Comedy, Drama, or Animation, at $3 or less SELECT * FROM movie_rentals WHERE (genre = 'Comedy' OR genre = 'Drama' OR genre = 'Animation') AND rental_price <= 3;
Common mistakes
Only parenthesizing one OR group when there are two — if you have two separate OR clusters joined by AND, both need their own parentheses, not just the first one.
Assuming NOT only applies to the next condition — NOT in front of a parenthesized group applies to the entire group's result, not just the first comparison inside it.
Misplacing a parenthesis — moving even one parenthesis can silently change which conditions get grouped together, and SQL won't warn you; it will just return the wrong rows.
Over-nesting when IN would do — three or more ORs comparing the same column to different values can usually be replaced with a single IN (...) list, which is easier to read and just as correct.
SQL Nested AND/OR Exercises
Apply what you learned above. Write a query that matches each task.
Table Schema
Task
Tip: Ctrl + Enter to submit