UPDATESQL

Unit 5, lesson 1 · CASE WHEN, ELSE

Choosing a value with CASE WHEN

Which products ship free?

CASE picks a value row by row. CASE WHEN condition THEN value ELSE other_value END gives the first value on rows where the condition is true and the other value everywhere else. It works anywhere a column can go, so you name it with AS like any other column. Leave out ELSE and the rows that do not match get NULL.

Example
SELECT name, category,
       CASE WHEN category = 'Audio' THEN 'sound'
            ELSE 'other'
       END AS department
FROM products;

Your task

Orders of $50 or more ship free. For every product, return the name, the price and a column called shipping that says 'free' when the price is 50 or more and 'standard' otherwise.

Tables you will use: products

Try it in your browserFree, no sign-up. Your query is checked as you go.