Before you start, you need
Combine complete conditions
A question can have several requirements. An available accessory must be in the Accessories category and be in stock. A customer from London or Bristol can live in either city.
AND requires both conditions to be met. OR accepts at least one. NOT negates a condition: NOT category = 'Audio' looks for products that are not Audio.
Usa parentheses to group the alternatives. (category = 'Accessories' OR category = 'Displays') AND stock > 0 requires stock for both categories. If you put the availability check next to Displays only, an out-of-stock accessory could also get in.
SQL evaluates AND before OR; the parentheses make the intent visible. Each alternative must be a complete condition: repeat the category comparison on both sides of OR. The practice tasks include an out-of-stock accessory so you can check the scope of the filter.
WHERE condition_1 AND condition_2
-- You can also use OR, NOT and parentheses.See it in action
We want accessories or displays, as long as they are in stock.
SELECT name, stock
FROM products
WHERE (category = 'Accessories' OR category = 'Displays')
AND stock > 0;| name | stock |
|---|---|
| Mouse | 18 |
| Keyboard | 7 |
| Monitor 24 | 4 |
| Monitor 27 | 2 |
| Cable USB-C | 30 |
- The parentheses express two accepted categories. AND additionally requires stock greater than 0.
- Monitor 24 is in Displays and has stock. An out-of-stock accessory would not get through, because availability is required for both alternatives.
- Each side of OR needs a complete condition. category = 'Accessories' OR 'Displays' does not express two comparisons.
NOT (category = 'Audio')negates the equality check and keeps the other categories. Here too, write the text in single quotes.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Require both category and stock
Write a query that returns name and stock for products in Accessories that have stock greater than 0.
Expected result: 3 rows with the columns name, stock, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
The category and the availability must both be met.
Combine them with AND.
The second condition is stock > 0.
Solution explained
SELECT name, stock FROM products WHERE category = 'Accessories' AND stock > 0;AND requires the Accessories category and positive stock. Adapter, which is out of stock, is left out even though it belongs to that category.
Group the alternatives
Guided practice
Group the alternatives
Show name and stock for accessories or displays that are available. Require stock greater than 0 for both categories.
Expected result: 5 rows with the columns name, stock, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
Look for a row that meets the category but not the availability.
Group the alternatives, then require stock.
You can use IN, or two equality checks joined by OR inside parentheses.
Solution explained
SELECT name, stock FROM products WHERE (category = 'Accessories' OR category = 'Displays') AND stock > 0;The parentheses group the two categories; the stock filter applies to both.
Negate a category
Guided practice
Negate a category
Show name and category for products that are not Audio. Use NOT.
Expected result: 7 rows with the columns name, category, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Which category should be left out and which word reverses a condition?
NOT goes in front of the whole condition: NOT category = ...
The condition is NOT category = 'Audio'.
Solution explained
SELECT name, category FROM products WHERE NOT category = 'Audio';NOT reverses the equality check. Since category does not allow NULL here, every row is either included or excluded.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Change the scope of the filter
Show name and stock for all accessories, in stock or not, or for displays with stock greater than 0.
Expected result: 6 rows with the columns name, stock, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
The stock requirement applies to only one category.
Separate two complete alternatives with OR.
Group the condition on Displays with stock > 0.
Solution explained
SELECT name, stock FROM products WHERE category = 'Accessories' OR (category = 'Displays' AND stock > 0);The out-of-stock accessory appears: availability is required only for Displays.
Key takeaways
- AND requires both conditions.
- OR accepts at least one alternative.
- NOT negates a condition.
- Parentheses make the scope explicit.