CASE WHEN
Turn a number into a word people can actually read
0 of 3 done · Lesson 7 of 8
Understand
Nobody outside finance wants to read a column of amounts. They want to know which orders are big ones. That means turning a number into a word, and doing it inside the query rather than in a spreadsheet afterwards, so the rule lives somewhere everyone can see it. CASE WHEN is how SQL writes if this then that.
SELECT a_column,
CASE WHEN a_test THEN a_label
ELSE another_label
END AS what_to_call_it
FROM where_it_lives;SELECTwhich columns you wantCASE WHENthe condition to testTHENthe label to give itELSEeverything that did not matchENDclose the caseFROMwhich table
The tables you are working with
8 rows
| id | customer | region | amount |
|---|---|---|---|
| 2101 | Northwind | Otago | 1250 |
| 2102 | Kea Ltd | Waikato | 800 |
| 2103 | Halcyon | Otago | 2110 |
| 2104 | Brightsmith | Auckland | 500 |
4 more rows in the table. Your query sees all 8.