Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 55

CASE Expression in MySQL

In this lesson: Use CASE WHEN in MySQL SELECT, UPDATE and ORDER BY — grade calculation example with output. Copy the query. In Hindi & English.

What is CASE

The CASE expression adds if-else logic inside a query. It checks conditions in order and returns a value for the first true one, otherwise a default. It is perfect for turning raw data into meaningful labels — like converting marks into grades — right in the SELECT.

💡 CASE is like if-else for SQL: pick a value based on conditions, computed per row.

The CASE syntax

CASE
    WHEN condition1 THEN value1
    WHEN condition2 THEN value2
    ELSE default_value
END

Each WHEN is checked top to bottom; the first true one gives its value. If none match, ELSE is used. You usually place it in the SELECT list with AS to name the column.

Example: assign grades

SELECT name, marks,
    CASE
        WHEN marks >= 90 THEN 'A'
        WHEN marks >= 75 THEN 'B'
        WHEN marks >= 40 THEN 'C'
        ELSE 'Fail'
    END AS grade
FROM students;
+------+-------+-------+ | name | marks | grade | +------+-------+-------+ | Aman | 88 | B | | Diya | 95 | A | | Ravi | 30 | Fail | +------+-------+-------+

Each student's marks are turned into a grade by the CASE, without changing the stored data.

Categorising data

SELECT name, salary,
    CASE
        WHEN salary >= 50000 THEN 'High'
        WHEN salary >= 25000 THEN 'Medium'
        ELSE 'Low'
    END AS pay_band
FROM employees;

CASE is great for grouping continuous values into named bands — salary into High/Medium/Low, age into ranges, and so on.

CASE in ORDER BY

SELECT name, status
FROM orders
ORDER BY
    CASE status
        WHEN 'urgent' THEN 1
        WHEN 'normal' THEN 2
        ELSE 3
    END;

Here CASE gives each status a sort rank, so urgent orders come first, then normal, then others — a custom order you cannot get from plain ASC/DESC.

Simple CASE form

SELECT name,
    CASE grade
        WHEN 'A' THEN 'Excellent'
        WHEN 'B' THEN 'Good'
        ELSE 'Average'
    END AS remark
FROM students;

The simple CASE compares one column to values. It is shorter when you are checking a single column against fixed values, rather than writing full conditions.

Common mistakes

  • Forgetting END. Every CASE must finish with END.
  • Wrong condition order. Put stricter conditions first, or a looser one catches everything.
  • Missing ELSE. Without it, unmatched rows return NULL.
  • Expecting CASE to change data. It only affects the query result.

Summary

  • CASE adds if-else logic inside a query.
  • Syntax: CASE WHEN ... THEN ... ELSE ... END.
  • Conditions are checked in order; the first true one wins.
  • Use it for grades, categories, and custom ORDER BY.
  • It computes values for display, not stored changes.

Next, handle nulls with IFNULL and COALESCE, or build a report in the report card query.

Frequently Asked Questions

What is the CASE expression in MySQL?
The CASE expression adds if-else logic inside an SQL query. It checks conditions in order and returns a value for the first one that is true, otherwise a default. It is used to create computed columns, like turning marks into grades.
How do I write a CASE expression?
Use CASE WHEN condition THEN value ... ELSE default END. Each WHEN is checked in order; the first true one gives its value. Put it in the SELECT list, often with AS to name the new column.
What is the difference between simple and searched CASE?
A searched CASE tests full conditions in each WHEN (like WHEN marks >= 90). A simple CASE compares one expression to values (like CASE grade WHEN 'A' THEN ...). Searched CASE is more flexible.
Can I use CASE in ORDER BY?
Yes. Putting a CASE expression in ORDER BY lets you sort by custom logic, such as showing a chosen status first, then the rest. It gives you full control over the sort order beyond simple ascending or descending.
Does CASE change the data in the table?
No. CASE only affects the result of the query; it does not modify the stored data. It computes a value for display or sorting at query time, leaving the underlying table unchanged.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicAggregate Functions in MySQL Your progress: 55 / 151 (36%) · Lesson 56 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free