# CASE Expressions

> Turn values into labels with CASE, and learn what happens with no ELSE, with NULL, and with the order of WHEN lines.

Subject: SQL | Lesson 12 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/case-expressions
Previous: [Expressions & Functions](https://codewithanimation.in/sql/expressions-and-functions.md) | Next: [INNER JOIN](https://codewithanimation.in/sql/inner-join.md)

## CASE is if and else for a value

`CASE` picks a result by testing conditions in order. Each `WHEN` holds a test and a `THEN` result. The first test that is true decides the answer, and the rest are skipped.

`ELSE` gives the result when no test is true. If you leave `ELSE` out and nothing matches, the result is `NULL`.

There is also a short form, `CASE team WHEN 'Red' THEN 1 WHEN 'Blue' THEN 2 END`, which compares one value with each `WHEN` value.

## Labels without extra code

`CASE` lets a query return a ready-made label, such as a score band, instead of making every program repeat the same rules. It works anywhere an expression works.

## Build score bands

```sql
SELECT name,
       CASE WHEN points >= 30 THEN 'high'
            WHEN points >= 20 THEN 'mid'
            ELSE 'low'
       END AS band
FROM players;
```

Gus has 40 points, so the first test is true and he is `high`. Fay has 24: the first test is false, the second is true, so she is `mid`.

Chen has `NULL` points. Both tests are unknown, which is not true, so no `WHEN` matches and Chen falls through to `ELSE`. Chen is called `low`, which is probably not what you meant.

## Common mistake: Order and NULL

The first true `WHEN` wins. If you write `points >= 20` before `points >= 30`, a player with 40 points matches the first one and is called `mid`. Put the most specific test first.

A `NULL` falls into `ELSE`, and with no `ELSE` it gives `NULL`. Decide what a missing value should be called, and test for it first with `IS NULL`.

## Tip: Always write ELSE, and test NULL first

Write `ELSE` even when you think it cannot happen, so the result never silently becomes NULL. If missing values are possible, make `WHEN points IS NULL` the first line.

## Try it: Compare four CASE versions

Switch between the versions and watch how the band changes for Gus, for Chen and for the players in between.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/case-expressions

## Check yourself

### Question 1

`CASE WHEN points >= 30 THEN 'high' END` is run for a player with 24 points. What is the result?

- A. NULL
- B. An empty text
- C. An error

Correct answer: A

No test is true and there is no ELSE, so CASE returns NULL.

- Why not B: With no matching WHEN and no ELSE, the result is NULL, not an empty text.
- Why not C: CASE without ELSE is valid. It gives NULL when nothing matches.

### Question 2

Bands are `>= 30` high, `>= 20` mid, ELSE low. Chen's points are NULL. What band does Chen get?

- A. low, because both tests are unknown and fall through to ELSE
- B. NULL, because the points are NULL
- C. high, because NULL is the largest value

Correct answer: A

An unknown test is not true, so no WHEN matches and the ELSE result is used.

- Why not B: ELSE exists, so the result is the ELSE value, not NULL.
- Why not C: NULL is not compared like that inside CASE. The test is unknown, which does not match.

### Question 3

`CASE WHEN points >= 20 THEN 'mid' WHEN points >= 30 THEN 'high' ELSE 'low' END` is run for a player with 40 points. What is the result?

- A. mid
- B. high
- C. low

Correct answer: A

WHEN lines are checked from the top. The first true one decides the result, so the specific test must come first.

- Why not B: The first true WHEN wins, and 40 >= 20 is checked first.
- Why not C: ELSE is used only when no WHEN is true.

## Remember this

- CASE checks WHEN tests from the top and the first true one decides the result.
- With no ELSE and no true WHEN, CASE returns NULL.
- A test on a NULL value is unknown, so it does not match and the row falls through to ELSE.
- Put the most specific test first, and test IS NULL before the other tests when values can be missing.
