đ The CASE expression in SQL is used to implement conditional logic within a query. It works similarly to an if-else or switch statement in programming languages, allowing you to return different values based on specified conditions. The CASEexpression is commonly used for categorizing data, creating calculated columns, custom sorting, and conditional aggregation.
đ What is the CASE Expression?
The CASE expression evaluates one or more conditions in order. As soon as a condition evaluates to TRUE, SQL returns the corresponding result and stops checking the remaining conditions. If none of the conditions match, the optional ELSE clause is returned.
Information
đ Types of CASE Expressions
SQL supports two forms of the CASE expression:
- đš Simple CASE Expression
- đš Searched CASE Expression
1ī¸âŖ Simple CASE Expression
A simple CASE compares a single expression against multiple possible values.
Simple CASE Syntax
CASE expression
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE default_result
ENDExample: Department Names
Simple CASE Example
SELECT
Name,
Department,
CASE Department
WHEN 'CS' THEN 'Computer Science'
WHEN 'IT' THEN 'Information Technology'
WHEN 'ME' THEN 'Mechanical Engineering'
ELSE 'Other Department'
END AS DepartmentName
FROM Students;2ī¸âŖ Searched CASE Expression
A searched CASE evaluates one or more Boolean conditions and is the more flexible form of the expression.
Searched CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
ENDExample: Grade Classification
Grade Classification
SELECT
Name,
Marks,
CASE
WHEN Marks >= 90 THEN 'A'
WHEN Marks >= 80 THEN 'B'
WHEN Marks >= 70 THEN 'C'
WHEN Marks >= 60 THEN 'D'
ELSE 'F'
END AS Grade
FROM Students;đ Sample Table
Assume the following Students table:
| StudentID | Name | Department | Marks |
|---|---|---|---|
| 101 | Alice | Computer Science | 92 |
| 102 | Bob | Mathematics | 85 |
| 103 | Charlie | Physics | 72 |
| 104 | David | Computer Science | 58 |
đˇī¸ CASE in the SELECT Statement
One of the most common uses of CASE is creating new calculated columns based on conditions.
Pass or Fail Status
SELECT
Name,
Marks,
CASE
WHEN Marks >= 50 THEN 'Pass'
ELSE 'Fail'
END AS Result
FROM Students;đ CASE in the WHERE Clause
Although less common, CASE can also be used inside the WHERE clause when conditional filtering is required.
CASE in WHERE
SELECT *
FROM Students
WHERE
CASE
WHEN Department = 'Computer Science'
THEN Marks >= 90
ELSE Marks >= 80
END;đ CASE in ORDER BY
CASE can customize the sorting order of query results.
Custom Sorting
SELECT Name,
Department
FROM Students
ORDER BY
CASE
WHEN Department = 'Computer Science' THEN 1
WHEN Department = 'Mathematics' THEN 2
ELSE 3
END;đ CASE with Aggregate Functions
Combining CASE with aggregate functions enables conditional calculations and reporting.
Conditional Counting
SELECT
COUNT(
CASE
WHEN Marks >= 90 THEN 1
END
) AS ExcellentStudents
FROM Students;Conditional SUM
SELECT
SUM(
CASE
WHEN Department = 'Computer Science'
THEN Marks
ELSE 0
END
) AS TotalCSMarks
FROM Students;đ CASE with UPDATE
The CASE expression can also be used to update values based on conditions.
Update Grades
UPDATE Students
SET Grade =
CASE
WHEN Marks >= 90 THEN 'A'
WHEN Marks >= 80 THEN 'B'
WHEN Marks >= 70 THEN 'C'
ELSE 'D'
END;đ Simple CASE vs Searched CASE
| Feature | Simple CASE | Searched CASE |
|---|---|---|
| Comparison Type | Compares one expression with values. | Evaluates Boolean conditions. |
| Flexibility | Limited | Highly flexible |
| Best Used For | Matching exact values. | Ranges and complex logic. |
| Most Common | No | Yes |
đŧ Real-World Example
A university wants to display the performance level of each student instead of showing only numerical marks.
Student Performance Report
SELECT
StudentID,
Name,
Marks,
CASE
WHEN Marks >= 90 THEN 'Excellent'
WHEN Marks >= 75 THEN 'Good'
WHEN Marks >= 50 THEN 'Average'
ELSE 'Needs Improvement'
END AS Performance
FROM Students
ORDER BY Marks DESC;This query converts raw marks into meaningful performance categories that are easier to understand in reports.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ CASE adds conditional logic to SQL queries.
- đ SQL supports both simple and searched CASE expressions.
- đ Conditions are evaluated from top to bottom.
- đ The first matching condition determines the returned value.
- đ ELSE is optional but recommended.
- đ CASE can be used with SELECT, WHERE, ORDER BY, UPDATE, and aggregate functions.