Module 5 · Advanced queries

Create categories with CASE

By the end you will be able to create labels according to the conditions of each row.

About 12 min

0 of 52 lessons

Before you start, you need

Combine results with UNION

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.

Minimal syntax
CASE WHEN condition THEN value
     ELSE other_value END AS label

See 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?

SQL
SELECT name,
       CASE
         WHEN stock = 0 THEN 'out_of_stock'
         ELSE 'available'
       END AS availability
FROM products;
Example result: 8 of 9 rows
nameavailability
Mouseavailable
Keyboardavailable
Monitor 24available
Pro Laptopout_of_stock
Headphonesavailable
Monitor 27available
SSD Driveavailable
Cable USB-Cavailable
  1. CASE tests stock = 0 on each row. If it is true, it returns out_of_stock; if not, it uses available.
  2. The alias availability names the new column. The query leaves stock unchanged in the table.
  3. 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.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Classify three bands

Not started

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.

Key takeaways

  • CASE calculates one value per row.
  • The first true WHEN is used.
  • ELSE covers the other cases.
  • END ends the expression.

Next: Transform text with functions