Before you start, you need
Combine conditions with AND, OR and NOT · Filter ranges and lists with BETWEEN and IN · Find text patterns with LIKE · Work correctly with NULL values
The problem
You will prepare two lists. The first asks for products in certain categories with a stock level within a range. The second asks for customers whose name matches a pattern or who have no email address.
Before writing SQL, break the question down into conditions. Decide which column each one uses and whether they must all be met, or one is enough. Then choose which columns the answer needs.
Test the filter with three cases: a row that should get in, one that should stay out and one right on the boundary. For example, if 1 to 10 units are allowed, 1 and 10 must also get in.
You can look at the tables and ask for hints if you need them. In the challenge the question will change; apply the same way of thinking instead of copying the previous filter.
How to think about it
Is the right category enough when a stock range is also required?
- Split the rule into an accepted category and a stock range. Both must be met.
- Check one row that is included, one excluded by category and one excluded by quantity. The exact values 1 and 10 must be accepted.
- For the customer challenge, decide whether one reason is enough to include someone or whether they must meet both.
Guided practice
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
Combine categories and stock
Write a query that returns name, category and stock for Accessories or Displays products whose stock is between 1 and 10.
Expected result: 3 rows with the columns name, category, stock, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
There are two requirements: an accepted category and stock within the range.
Handle the stock with BETWEEN.
Combine both filters with AND.
Solution explained
SELECT name, category, stock FROM products WHERE category IN ('Accessories', 'Displays') AND stock BETWEEN 1 AND 10;Keyboard (7), Monitor 24 (4) and Monitor 27 (2) meet both the category and the range. Mouse (18) and Cable USB-C (30) are above 10.
Challenge
Solve this variation. Spot the part that has changed before you look for help.
Practice
Pattern or missing value
Show name and email for customers whose name starts with M or who have no email.
Expected result: 4 rows with the columns name, email, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
There are two independent reasons to keep a row.
You need a pattern condition and a missing-value condition joined as alternatives.
LIKE checks the name and IS NULL checks the email.
Solution explained
SELECT name, email FROM customers WHERE name LIKE 'M%' OR email IS NULL;The missing-value alternative keeps David and Peter even though their names do not start with M.
Key takeaways
- Turn each requirement into a condition.
- Check a boundary and an excluded row.
- Use AND or OR as the question requires.
Next: Sort and limit results