Before you start, you need
Return a label based on a condition
A quantity can become a label that is easier to read. If a product has 0 units, we want to see it as out_of_stock; if it has some, as available. CASE calculates that label without changing the stock.
WHEN indicates the condition and THEN the value it returns when the condition is met. ELSE gives the alternative and END ends the expression. Then you can use AS to give the column a name.
If you write several WHEN, the one used is the first one that is true. To tell out_of_stock, low and normal apart, first think about their limits: 0, 1 to 5, and 6 or more. Arrange the conditions so that each case gets the intended label.
Without ELSE, a row that meets no condition gets NULL. Add an alternative if you need to classify every product, and check values that sit right on the limits.
CASE WHEN condition THEN value
ELSE other_value END AS labelSee it in action
How can we mark each product as out_of_stock or available?
Before running: Which label will a product with stock 0 get, and which will one with stock 1 get?
SELECT name,
CASE
WHEN stock = 0 THEN 'out_of_stock'
ELSE 'available'
END AS availability
FROM products;| name | availability |
|---|---|
| Mouse | available |
| Keyboard | available |
| Monitor 24 | available |
| Pro Laptop | out_of_stock |
| Headphones | available |
| Monitor 27 | available |
| SSD Drive | available |
| Cable USB-C | available |
- CASE tests stock = 0 on each row. If it is true, it returns out_of_stock; if not, it uses available.
- The alias availability names the new column. The query leaves stock unchanged in the table.
- With more bands, the first true WHEN is used. In the challenge, decide the labels for 0, 1, 5 and 6 before ordering the conditions.
- Without ELSE, a row that is not covered gets NULL. Add an alternative when the question must classify every row.
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
Classify the stock
Write a query that returns name and a column called availability: 'out_of_stock' when stock is 0 and 'available' otherwise.
Expected result: 9 rows with the columns name, availability, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
First, tell zero stock apart from every other value.
Add THEN and ELSE.
Finish with END AS availability.
Solution explained
SELECT name, CASE WHEN stock = 0 THEN 'out_of_stock' ELSE 'available' END AS availability FROM products;The expression produces one label per row without modifying the stock.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Classify three bands
Show name and a CASE with the alias level: 'out_of_stock' for stock 0, 'low' for stock from 1 to 5 and 'normal' otherwise.
Expected result: 12 rows with the columns name, level, 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.
Decide the label for each boundary before writing the code.
Order the conditions so an out-of-stock product is not classified as low.
Finish with ELSE and END AS level.
Solution explained
SELECT name, CASE WHEN stock = 0 THEN 'out_of_stock' WHEN stock <= 5 THEN 'low' ELSE 'normal' END AS level FROM products;The condition for 0 is evaluated first; then <= 5 covers the remaining values from 1 to 5.
Key takeaways
- CASE calculates one value per row.
- The first true WHEN is used.
- ELSE covers the other cases.
- END ends the expression.