SQL Topics
CASE Expression
title: CASE Expression
In the previous lesson, you learned about:
PIVOT
UNPIVOTwhich are used for transforming data between rows and columns.
Now we move to one of the most commonly used SQL features:
CASE allows SQL queries to make decisions based on conditions.
Think of CASE as SQL's version of:
IF-ELSE
SWITCH
CONDITIONAL LOGICCASE is heavily used in:
Reporting
Analytics
Dashboards
Data Transformation
Business Rulesand is one of the most frequently asked SQL interview topics.
What is CASE Expression?
The CASE Expression allows SQL to evaluate conditions and return different values based on those conditions.
In simple words:
IF Condition True
↓
Return Value A
ELSE
↓
Return Value BSimple Definition
CASE is a conditional expression that returns different results based on specified conditions.
Why is CASE Needed?
Suppose we have:
| Employee | Salary |
|---|---|
| Rahul | 40000 |
| Priya | 70000 |
| Amit | 90000 |
Requirement:
Salary < 50000
Low Salary
Salary >= 50000
High SalaryCASE can generate this classification automatically.
Types of CASE Expression
SQL supports:
Simple CASE
Searched CASESimple CASE
Compares one expression against multiple values.
Syntax
Example
Output
| Employee | Department |
|---|---|
| Rahul | HR |
| Priya | IT |
Understanding Simple CASE
Evaluation:
DepartmentID = 1
↓
HR
DepartmentID = 2
↓
IT
DepartmentID = 3
↓
FinanceSearched CASE
More powerful.
Uses conditions.
Syntax
Example
Output
| Employee | Salary | Category |
|---|---|---|
| Rahul | 40000 | Low Salary |
| Priya | 70000 | Medium Salary |
| Amit | 90000 | High Salary |
How CASE Works
Execution:
Condition 1
↓
True?
↓
Return Result
False?
↓
Check Next ConditionELSE Clause
The ELSE block handles unmatched conditions.
Example:
Without ELSE
Unmatched rows return:
NULLCASE in SELECT
Most common usage.
Example:
CASE in ORDER BY
Custom sorting.
Example:
CASE in WHERE Clause
Example:
CASE in UPDATE
Example:
Output
Bonus assigned automatically.
CASE in Aggregate Functions
Very common interview topic.
Count Employees by Category
Example Data
| Salary |
|---|
| --------- |
| 40000 |
| 70000 |
| 90000 |
Result:
2Conditional Aggregation
Example:
Student Grade Example
Marks Table:
| Student | Marks |
|---|---|
| Rahul | 95 |
| Priya | 80 |
| Amit | 60 |
Query
Output
| Student | Grade |
|---|---|
| Rahul | A |
| Priya | B |
| Amit | C |
Banking Example
Requirement:
Classify AccountsE-Commerce Example
Requirement:
Order StatusPayroll Example
Requirement:
Tax SlabsUniversity Example
Requirement:
Attendance StatusNested CASE
CASE inside CASE.
Example:
CASE vs IF
| Feature | CASE | IF |
|---|---|---|
| SQL Standard | Yes | No |
| Multiple Conditions | Yes | Limited |
| Portability | High | Low |
| Readability | Better | Moderate |
Advantages of CASE
Conditional Logic
Primary purpose.
SQL Standard
Supported by almost all databases.
Flexible
Works in SELECT, WHERE, ORDER BY, UPDATE.
Reporting Friendly
Widely used in dashboards.
Supports Aggregations
Conditional calculations become easy.
Disadvantages
Complex Queries
Large CASE blocks become difficult to read.
Performance Impact
Many conditions may increase processing.
Nested CASE Complexity
Harder to maintain.
Common Mistakes
Missing ELSE
May return NULL unexpectedly.
Incorrect Condition Order
Conditions evaluated top to bottom.
Overusing Nested CASE
Reduces readability.
Using CASE Instead of Proper Design
Not always the best solution.
Best Practices
Always Include ELSE
Handle unexpected values.
Keep Conditions Ordered
Most specific conditions first.
Use Meaningful Aliases
Example:
AS SalaryCategory
AS EmployeeGradeAvoid Deep Nesting
Simplify logic when possible.
Test Edge Cases
Verify all conditions.
Common Interview Questions
What is CASE Expression?
A conditional SQL expression that returns values based on conditions.
What is the difference between Simple CASE and Searched CASE?
Simple CASE compares values.
Searched CASE evaluates conditions.
Can CASE be used in WHERE?
Yes.
Can CASE be used with aggregate functions?
Yes.
Very commonly.
What happens if ELSE is omitted?
Unmatched rows return NULL.
Summary
CASE Expression is one of the most important SQL features for implementing conditional logic. It allows queries to classify data, perform conditional calculations, create custom reports, and apply business rules directly within SQL.
In this lesson, you learned:
- What CASE Expression is
- Simple CASE
- Searched CASE
- CASE in SELECT
- CASE in WHERE
- CASE in UPDATE
- Conditional Aggregation
- Real-world examples
- Advantages and disadvantages
- Best practices
Mastering CASE Expressions is essential because they are used extensively in reporting, analytics, business intelligence, and SQL interviews.
Next Step
Continue to the next lesson:
MERGE Statement →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for CASE Expression.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, subqueries, case
Related SQL Topics