Skip to content

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
First rows of the orders table
idcustomerregionamount
2101NorthwindOtago1250
2102Kea LtdWaikato800
2103HalcyonOtago2110
2104BrightsmithAuckland500

4 more rows in the table. Your query sees all 8.