Class 29 of 58 • Operators, Patterns & Functions

SQL CASE Expression: IF-THEN-ELSE Logic in Queries

SQL is a declarative language, but real-world reporting frequently requires conditional if-then-else branching logic (e.g., "If GPA >= 3.8, label as 'High Distinction'; if GPA >= 3.5, label as 'First Class'"). The CASE expression is SQL's universal way to implement conditional logic across SELECT, WHERE, and ORDER BY clauses.


The Searched CASE Expression Syntax

The searched CASE expression evaluates boolean conditions sequentially from top to bottom. It returns the THEN result for the first condition that evaluates to TRUE:

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

Hands-on Example: Categorizing Student GPA Grades

SELECT 
    student_id,
    first_name,
    gpa,
    CASE 
        WHEN gpa >= 3.80 THEN 'High Distinction'
        WHEN gpa >= 3.50 THEN 'First Class with Honors'
        WHEN gpa >= 3.00 THEN 'Second Class'
        ELSE 'Needs Improvement'
    END AS academic_standing
FROM students;
student_id first_name gpa academic_standing
2 Pooja 3.92 High Distinction
3 Aman 3.85 High Distinction
1 Rahul 3.75 First Class with Honors
4 Neha 3.68 First Class with Honors

Custom Sorting with CASE in ORDER BY

You can dictate custom, non-alphabetical sorting sequences using CASE:

-- Custom priority sorting: Urgent tasks first, then Normal, then Low
SELECT task_name, priority
FROM project_tasks
ORDER BY 
    CASE priority
        WHEN 'Urgent' THEN 1
        WHEN 'High' THEN 2
        WHEN 'Normal' THEN 3
        ELSE 4
    END;

Congratulations! You have completed Module 4. Now let us advance to Module 5 and learn summary statistics starting with SQL Aggregate Functions!


Frequently Asked Questions (FAQ)

A Simple CASE compares a single expression to specific static values (CASE status WHEN 'A' THEN ...). A Searched CASE evaluates independent boolean expressions in each WHEN clause (CASE WHEN score >= 90 THEN ...), making it far more flexible.

If no condition evaluates to TRUE and no ELSE clause is provided, the CASE expression returns NULL.

Yes! Developers frequently use CASE inside ORDER BY to implement custom sorting orders (e.g., displaying 'Pending' items first, followed by 'Approved', then 'Rejected').

Next Step in Your SQL Database Learning Path:

Proceed to the next class in sequence to build production mastery of relational database architecture.

Continue to Class 30: Aggregate Functions → Previous: ← Date & Time Functions | Full Syllabus


Related SQL Masterclass Lessons & Recommended Next Topics

SELECT Query Expressions →

Creating dynamic derived columns with CASE expressions.

Conditional Aggregation →

Pivoting rows into columns using SUM(CASE WHEN ...).

Batch Updates with CASE →

Updating multiple rows with different values in one query.

Boolean Expressions →

Constructing complex boolean branching logic.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!