LESSON 23 / 62 · Conditional logicConditional logic, CASE statement
Conditional logic in SQL using the CASE WHEN THEN END statement
SQL, like many programming languages, lets you write conditional logic so that a query's behavior depends on the values of certain columns or expressions. In this article, we'll look at how this is implemented in SQL using the CASE operator.
Concept of conditional logic
The concept of conditional logic means that the program has several execution paths depending on some condition.
The Schedule database has a Student table with a birthday field that reflects the student's date of birth. Let's say in the selection, it is necessary to display not the date of birth itself, but the text value "Adult" or "Minor", depending on whether the student is 18 or older. This is an example of conditional logic, in which either one value or another should be displayed depending on the specific condition.
idINTidINTidINTidINTidINTidINTidINT
SELECT first_name, last_name,
CASE
WHEN TIMESTAMPDIFF(YEAR, birthday, NOW()) >= 18 THEN 'Adult'
ELSE 'Minor'
END AS status
FROM Student| first_name | last_name | status |
|---|---|---|
| Nikolaj | Sokolov | Adult |
| Vyacheslav | Eliseev | Adult |
| Ivan | Efremov | Adult |
| Anatolij | ZHdanov | Minor |
| Georgij | Noskov | Adult |
| Artyom | Sergeev | Minor |
| Arina | Evseeva | Adult |
| Angelina | Voroncova | Adult |
| Ekaterina | Ustinova | Adult |
| Raisa | Lapina | Adult |
| Leonid | Ignatov | Minor |
| Snezhana | Seliverstova | Adult |
| Semyon | Biryukov | Adult |
| Georgij | Baranov | Adult |
| YUliya | Vishnyakova | Adult |
| Valentina | Bolshakova | Adult |
| Leonid | Kryukov | Adult |
| Vladislav | Cvetkov | Adult |
| Snezhana | Morozova | Adult |
| Lyubov | Borisova | Adult |
| Anfisa | Kalashnikova | Adult |
| Anna | Osipova | Adult |
CASE search expression syntax
CASE
WHEN condition THEN result
[WHEN ...]
[ELSE default_result]
END- [ ]optional part
- ...can be repeated
The value returned when none of the conditions is met. Without ELSE, NULL is returned.
- condition
- A logical expression of the WHEN branch, for example price > 100.
- result
- The value CASE returns if the branch condition is true.
- default_result
- Optional
The WHEN branches are checked in order: if the condition of the first branch is true, the CASE expression returns its result, otherwise the next branch is checked and so on.
Example
Let's take the CASE operator to implement the stages of schooling.

SELECT name,
CASE
WHEN SUBSTRING(name, 1, INSTR(name, ' ')) IN (10, 11) THEN 'High School'
WHEN SUBSTRING(name, 1, INSTR(name, ' ')) IN (5, 6, 7, 8, 9) THEN 'Middle school'
ELSE 'Elementary school'
END AS stage
FROM Class| name | stage |
|---|---|
| 8 A | Middle school |
| 8 B | Middle school |
| 9 C | Middle school |
| 9 B | Middle school |
| 9 A | Middle school |
| 10 B | High School |
| 10 A | High School |
| 11 B | High School |
| 11 A | High School |
| 7 A | Middle school |
| 7 B | Middle school |
| 6 A | Middle school |
| 6 B | Middle school |
| 5 A | Middle school |
| 5 B | Middle school |
| 4 A | Elementary school |
SUBSTRING(name, 1, INSTR(name, ' '))- Next, we check for occurrences of this class number in the list of classes related to "High School" and "Middle school".
- If the class number doesn't match 5-11, we output "Elementary school".
Syntax of a simple CASE expression
The CASE operator also has a simpler syntax, which is similar to the CASE search expression, but is a little less flexible. Here is its syntax:
MySQL 8.4
CASE value
WHEN compare_value THEN result
[WHEN ...]
[ELSE default_result]
END- [ ]optional part
- ...can be repeated
A specific value of the WHEN branch, for example a number or a string.
The value CASE returns if value matches the value of the branch.
The value returned when there is no match. Without ELSE, NULL is returned.
- value
- The expression being checked, for example a table column.
- default_result
- Optional
In this syntax, the value in CASE is compared in turn with the values in WHEN, and if one matches, the value following THEN is returned.
Using this syntax, we can rewrite our previous example:
SELECT name,
CASE SUBSTRING(name, 1, INSTR(name, ' '))
WHEN 11 THEN 'High School'
WHEN 10 THEN 'High School'
WHEN 9 THEN 'Middle school'
WHEN 8 THEN 'Middle school'
WHEN 7 THEN 'Middle school'
WHEN 6 THEN 'Middle school'
WHEN 5 THEN 'Middle school'
ELSE 'Elementary school'
END AS stage
FROM Class| name | stage |
|---|---|
| 8 A | Middle school |
| 8 B | Middle school |
| 9 C | Middle school |
| 9 B | Middle school |
| 9 A | Middle school |
| 10 B | High School |
| 10 A | High School |
| 11 B | High School |
| 11 A | High School |
| 7 A | Middle school |
| 7 B | Middle school |
| 6 A | Middle school |
| 6 B | Middle school |
| 5 A | Middle school |
| 5 B | Middle school |
| 4 A | Elementary school |
Test yourself
What value will the CASE operator return in this case?
CASE 2
WHEN 0 THEN 'Zero'
WHEN 1 THEN 'One'
ELSE 'Many'
ENDPython Academy
Practice
Solve it in the editor. The query runs against a sample database in your browser.
To display the rating category, use the rating alias.
Write a query and run it to see the results.
Write a query and run it to see the results.