CASE expression

SQL's if-then-else: it returns one value when a condition holds and another otherwise.

In more detail

It turns a value into a label that can then be grouped on, such as 'out' when stock is zero and 'in' otherwise. Conditions are tried in order and the first true one wins; if none is true and there is no fallback, the result is NULL.

Example

CASE WHEN stock = 0 THEN 'out' ELSE 'in' END AS stock_state

Related terms

Taught in

References