CASE Statement
Learn how to write inline conditional logic in a SQL query using the CASE WHEN expression.
Introduction
Sometimes a query needs to make a decision based on a value — for example, turning a raw numeric score into a letter grade, or labeling an order as "Small," "Medium," or "Large." The CASE expression lets you write this kind of if/else logic directly inside a SQL statement.
In this lesson we will use a "students" table with a numeric marks column to build a CASE expression that converts marks into a letter grade.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
marks INT
);
INSERT INTO students (id, name, marks) VALUES
(1, 'Alex', 92),
(2, 'Priya', 78),
(3, 'Sam', 65),
(4, 'Jordan', 41);- The syntax of a CASE WHEN ... THEN ... ELSE ... END expression.
- How to use CASE to convert a numeric value into a descriptive label.
- How to chain multiple WHEN conditions together.
- How CASE can also be used inside an ORDER BY clause.
Why CASE Matters
Without CASE, converting a numeric score into a label like "A" or "B" would require pulling the raw data into an application and writing if/else logic there. CASE lets you do this transformation right inside the SELECT statement, so the labeled result comes straight out of the database.
CASE Syntax
A CASE expression checks a series of conditions in order, and returns the result tied to the first condition that is true. If none of the conditions match, the ELSE result is returned instead.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
ENDThe ELSE branch is optional. If you omit it and no WHEN condition matches, the CASE expression simply returns NULL.
Worked Example: Letter Grades
Let's use CASE to convert each student's numeric marks into a letter grade, directly inside a SELECT statement.
SELECT
name,
marks,
CASE
WHEN marks >= 90 THEN 'A'
WHEN marks >= 75 THEN 'B'
WHEN marks >= 60 THEN 'C'
WHEN marks >= 50 THEN 'D'
ELSE 'F'
END AS grade
FROM students;+--------+-------+-------+
| name | marks | grade |
+--------+-------+-------+
| Alex | 92 | A |
| Priya | 78 | B |
| Sam | 65 | C |
| Jordan | 41 | F |
+--------+-------+-------+MySQL checks each WHEN condition from top to bottom for every row and stops at the first one that is true. For Alex (92), the very first condition marks >= 90 is true, so the CASE expression immediately returns 'A' without checking the rest.
Conditions are evaluated in order, so always list the most specific or highest-priority condition first. If marks >= 60 were listed before marks >= 90, a score of 92 would incorrectly match the 60 condition first and return 'C' instead of 'A'.
CASE with Multiple Conditions
A WHEN clause can include more than one condition combined with AND or OR, letting you build more precise rules.
SELECT
name,
marks,
CASE
WHEN marks >= 50 AND marks < 60 THEN 'Needs Improvement'
WHEN marks < 50 THEN 'Failing'
ELSE 'Passing Well'
END AS status
FROM students;+--------+-------+----------------+
| name | marks | status |
+--------+-------+----------------+
| Alex | 92 | Passing Well |
| Priya | 78 | Passing Well |
| Sam | 65 | Passing Well |
| Jordan | 41 | Failing |
+--------+-------+----------------+Bonus: CASE Inside ORDER BY
As a bonus technique, CASE can also be used inside an ORDER BY clause to create a custom sort order that does not follow simple alphabetical or numeric rules — for example, always showing failing students first regardless of their exact marks.
SELECT name, marks
FROM students
ORDER BY
CASE WHEN marks < 50 THEN 0 ELSE 1 END,
marks DESC;+--------+-------+
| name | marks |
+--------+-------+
| Jordan | 41 |
| Alex | 92 |
| Priya | 78 |
| Sam | 65 |
+--------+-------+This ORDER BY first sorts by the CASE result (failing students get 0 and appear first), and within that, sorts by marks descending. Custom sorting with CASE is a handy technique to keep in mind, and is covered in more depth in later lessons on sorting.
Common Mistakes
- Ordering WHEN conditions incorrectly, causing an earlier condition to catch rows meant for a later one.
- Forgetting the END keyword, which is required to close every CASE expression.
- Omitting ELSE when every row should always get a result, leaving some rows as unexpected NULLs.
- Forgetting to alias the CASE expression with AS, leaving the result column awkwardly named.
Best Practices
- Order WHEN conditions from most specific to least specific, or highest threshold to lowest.
- Always include an ELSE branch unless a NULL result for unmatched rows is intentional.
- Keep CASE expressions readable by formatting each WHEN on its own line.
- Use descriptive aliases like AS grade or AS status for the resulting column.
Frequently Asked Questions
Can CASE be used outside of SELECT?
Yes, CASE can be used in SELECT, WHERE, ORDER BY, and even inside UPDATE statements — anywhere a value expression is expected.
What happens if multiple WHEN conditions are true?
Only the first matching condition, from top to bottom, is used. Any conditions after it are ignored for that row.
Is there a shorter form of CASE for simple equality checks?
Yes, MySQL supports CASE column WHEN value1 THEN result1 WHEN value2 THEN result2 END, which is a shorthand when you are always comparing the same column to specific values.
Does CASE work with aggregate functions like SUM() or COUNT()?
Yes, CASE is frequently combined with aggregate functions to count or sum values conditionally, which is a technique covered in a later lesson.
Key Takeaways
- CASE WHEN ... THEN ... ELSE ... END adds inline conditional logic to a query.
- Conditions are checked in order, and the first true condition wins.
- ELSE provides a fallback result when no condition matches.
- CASE can combine multiple conditions with AND or OR.
- CASE can also be used inside ORDER BY to build a custom sort order.
Summary
The CASE expression brings conditional, if/else-style logic directly into your SQL queries, letting you transform raw values into meaningful labels without any extra application code.
In this lesson, you learned CASE WHEN syntax, used it to convert numeric marks into letter grades, and saw how it can also power a custom ORDER BY. Next, you will begin learning about joins, starting with why they exist.
- You can write a CASE WHEN expression inside a SELECT statement.
- You understand how condition order affects the result.
- You are ready to move on to joins.