MySQL CASE: Return Values Based on Matches or Conditions

MySQL CASE returns a value based on either an exact match or a logical condition. It is commonly used inside SELECT, ORDER BY, aggregate expressions, and other SQL expressions.

SELECT id,
       name,
       mark,
       CASE
           WHEN mark >= 90 THEN 'A'
           WHEN mark >= 80 THEN 'B'
           WHEN mark >= 70 THEN 'C'
           ELSE 'FAIL'
       END AS grade
FROM student;
There are two main CASE forms: simple CASE compares one expression with values; searched CASE tests separate conditions. In both forms, MySQL returns the result from the first matching WHEN branch.
MySQL CASE expression using WHEN THEN and ELSE

Simple CASE: Match a Value Top ↑

Simple CASE evaluates one expression and compares it with each WHEN value.

CASE expression
    WHEN value1 THEN result1
    WHEN value2 THEN result2
    ELSE default_result
END
Important syntax correction: a CASE expression ends with END, not END CASE. END CASE belongs to stored-program CASE statements, not the CASE expression used inside SELECT.

Searched CASE: Test Conditions Top ↑

Searched CASE does not compare one expression with fixed values. Each WHEN contains its own condition.

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ELSE default_result
END

This form is appropriate for ranges, inequalities, NULL checks, and combinations of conditions.

CASE Stops at the First Matching WHEN Top ↑

CASE checks WHEN branches from top to bottom and returns the result from the first branch whose value or condition matches.

CASE
    WHEN mark >= 90 THEN 'A'
    WHEN mark >= 80 THEN 'B'
    WHEN mark >= 70 THEN 'C'
    ELSE 'FAIL'
END

A mark of 94 satisfies all three numeric comparisons, but the first true condition is mark >= 90, so the result is A.

Order matters. If the broadest condition is placed first, later branches may never be reached.

Example: Assign a Location by Class Top ↑

Use simple CASE when one column is being matched against several exact values:

SELECT id,
       name,
       class,
       mark,
       gender,
       CASE class
           WHEN 'Four' THEN '1st floor'
           WHEN 'Five' THEN '2nd floor'
           WHEN 'Three' THEN '2nd floor'
           WHEN 'Two' THEN '1st floor'
           ELSE 'Ground floor'
       END AS location
FROM student
ORDER BY id;
idnameclassmarkgenderlocation
1John DeoFour75male1st floor
2Max RuinThree85male2nd floor
3ArnoldThree55male2nd floor
4Krish StarFour60male1st floor
5John MikeFour60male1st floor
6Alex JohnFour55male1st floor
7My John RobFifth78maleGround floor
8AsruidFive85male2nd floor
9Tes QrySix78maleGround floor
10Big JohnFour55male1st floor
Notice that Fifth does not equal Five, so that row falls to ELSE. Exact data values matter in a simple CASE.

Example: Assign Grades by Mark Top ↑

Use searched CASE for numeric ranges:

SELECT id,
       name,
       class,
       mark,
       gender,
       CASE
           WHEN mark >= 90 THEN 'A'
           WHEN mark >= 80 THEN 'B'
           WHEN mark >= 70 THEN 'C'
           ELSE 'FAIL'
       END AS grade
FROM student
ORDER BY id;
idnameclassmarkgendergrade
1John DeoFour75maleC
2Max RuinThree85maleB
3ArnoldThree55maleFAIL
4Krish StarFour60maleFAIL
5John MikeFour60maleFAIL
6Alex JohnFour55maleFAIL
7My John RobFifth78maleC
8AsruidFive85maleB
9Tes QrySix78maleC
10Big JohnFour55maleFAIL
11RonaldSix89maleB
12ReckySix94maleA
13KtySeven88maleB

CASE with BETWEEN Top ↑

BETWEEN is inclusive at both ends, so non-overlapping ranges must be chosen carefully.

SELECT id,
       name,
       mark,
       CASE
           WHEN mark BETWEEN 90 AND 100 THEN 'A'
           WHEN mark BETWEEN 80 AND 89 THEN 'B'
           WHEN mark BETWEEN 70 AND 79 THEN 'C'
           ELSE 'FAIL'
       END AS grade
FROM student;

For integer marks this is clear. For decimal scores, threshold-style conditions such as mark >= 90, mark >= 80, and mark >= 70 often avoid gaps such as 89.5.

CASE with GROUP BY and SUM Top ↑

CASE is frequently used for conditional aggregation. The following query counts male and female students inside each class:

SELECT class,
       COUNT(*) AS total,
       SUM(
           CASE
               WHEN gender = 'male' THEN 1
               ELSE 0
           END
       ) AS male,
       SUM(
           CASE
               WHEN gender = 'female' THEN 1
               ELSE 0
           END
       ) AS female
FROM student
GROUP BY class
ORDER BY class;
classtotalmalefemale
Eight110
Five330
Four945
Nine211
Seven1055
Six725
Three321

This pattern converts each matching row into 1 and each non-matching row into 0, then SUM() adds those values within each GROUP BY group.

CASE with NULL Values Top ↑

Use IS NULL or IS NOT NULL inside a searched CASE.

SELECT id,
       CASE
           WHEN c_name IS NOT NULL THEN 'checked'
           ELSE 'not_checked'
       END AS my_status
FROM student;

Do not test NULL with = NULL or <> NULL. See SQL NULL values.

Why simple CASE does not match NULL with WHEN NULL Top ↑

This is not a reliable NULL test:

-- Do not use this to test NULL
CASE c_name
    WHEN NULL THEN 'not_checked'
    ELSE 'checked'
END

Use searched CASE with WHEN c_name IS NULL instead, because ordinary equality comparison with NULL does not evaluate to true.

ORDER BY a CASE Result Top ↑

A CASE expression can create a display value and the query can sort by its alias:

SELECT id,
       name,
       class,
       CASE class
           WHEN 'Four' THEN '1st floor'
           WHEN 'Five' THEN '2nd floor'
           WHEN 'Three' THEN '2nd floor'
           ELSE 'Ground floor'
       END AS location
FROM student
ORDER BY location, id;

See ORDER BY for deterministic sorting and tie-breakers.

Custom sort order with CASE Top ↑

If alphabetical order is not the desired business order, CASE can return numeric sort ranks:

SELECT id,
       name,
       class
FROM student
ORDER BY
    CASE class
        WHEN 'Three' THEN 1
        WHEN 'Four' THEN 2
        WHEN 'Five' THEN 3
        ELSE 4
    END,
    id;

What Happens without ELSE? Top ↑

ELSE is optional. If no WHEN branch matches and no ELSE is present, the CASE expression returns NULL.

SELECT id,
       mark,
       CASE
           WHEN mark >= 90 THEN 'A'
       END AS grade
FROM student;

Rows below 90 receive NULL in the grade result column.

Result Datatypes Top ↑

CASE can return numbers, strings, dates, or other expressions. MySQL determines a common result type from the possible branches.

Keep branch results semantically consistent when possible:

CASE
    WHEN mark >= 50 THEN 'Pass'
    ELSE 'Fail'
END

is clearer than mixing unrelated numeric and text meanings in the same CASE result.

CASE vs WHERE Top ↑

CASE usually returns a value; WHERE usually removes rows that do not satisfy a condition.

To label students:

SELECT name,
       CASE
           WHEN mark >= 70 THEN 'High'
           ELSE 'Other'
       END AS mark_group
FROM student;

To return only students with marks of 70 or more:

SELECT id, name, mark
FROM student
WHERE mark >= 70;

CASE vs IF() Top ↑

MySQL also provides the IF() function for a simple true/false choice:

SELECT id,
       name,
       IF(
           mark >= 50,
           'Pass',
           'Fail'
       ) AS result
FROM student;

CASE is usually clearer when several branches are required and is standard SQL syntax supported more broadly across database systems.

Common CASE Mistakes Top ↑

Writing END CASE inside a SELECT expression Top ↑

A CASE expression in SELECT ends with END. END CASE belongs to a different stored-program syntax.

Placing a broad condition before a narrow one Top ↑

WHEN mark >= 70 before WHEN mark >= 90 would classify 94 as the first matching branch. Put higher thresholds first.

Using BETWEEN ranges with gaps or overlaps Top ↑

BETWEEN includes both boundaries. Integer ranges such as 80-89 are fine for integer marks, but decimal values need carefully defined boundaries.

Comparing NULL with = NULL Top ↑

Use IS NULL or IS NOT NULL in a searched CASE.

Forgetting ELSE Top ↑

If no WHEN matches and ELSE is omitted, CASE returns NULL. That may be correct, but it should be intentional.

Using CASE when WHERE is the real requirement Top ↑

CASE labels or calculates values. WHERE filters rows. Choose the construct that expresses the actual intent.

Ignoring inconsistent source values Top ↑

A simple CASE comparing 'Five' will not match 'Fifth'. CASE should not be used to hide avoidable data-quality inconsistencies.

Video Tutorial Top ↑

Frequently Asked Questions Top ↑

Q1: What are the two types of CASE expression in MySQL?

Simple CASE compares one expression with values. Searched CASE evaluates separate conditions in each WHEN branch.

Q2: Does a CASE expression end with END or END CASE?

A CASE expression used in SELECT ends with END. END CASE is used in stored-program CASE statement syntax.

Q3: What happens when more than one WHEN condition is true?

CASE returns the result from the first matching WHEN branch, so branch order matters.

Q4: What happens if ELSE is omitted?

If no WHEN branch matches, the CASE expression returns NULL.

Q5: Can CASE be used with GROUP BY?

Yes. CASE is commonly placed inside SUM() or another aggregate for conditional aggregation within grouped results.

Q6: How do I test NULL inside CASE?

Use a searched CASE with IS NULL or IS NOT NULL. Do not compare a value with = NULL.

Q7: What is the difference between CASE and WHERE?

CASE normally returns or transforms a value, while WHERE filters rows from the result.


UNION Subqueries


Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer