Beginner

334. CASE Expression

Learn how to create conditional logic inside SQL queries.

📘 Lesson 334 of 728 ⏱ 12 min 🎯 Beginner

The CASE expression evaluates conditions and returns different values depending on which condition is satisfied.

CASE is one of the most important SQL tools for creating business rules, categories, flags, and conditional calculations.

Classify employees into salary bands based on their salary.

SQL
SELECT
    EmployeeName,
    Salary,
    CASE
        WHEN Salary >= 100000 THEN 'High'
        WHEN Salary >= 50000 THEN 'Medium'
        ELSE 'Low'
    END AS SalaryBand
FROM Employees;

CASE is widely used in reporting, customer segmentation, financial analysis, risk classification, KPI calculations, and data transformation.

  • CASE provides conditional logic.
  • It supports multiple WHEN conditions.
  • ELSE handles unmatched conditions.
  • It can create business categories.
  • CASE can be used in SELECT, ORDER BY, GROUP BY, and other expressions.
What is CASE in SQL?

CASE is a conditional expression used to return different values based on specified conditions.

Where can CASE be used?

CASE can be used in SELECT, WHERE-related expressions, ORDER BY, GROUP BY, aggregations, and calculated expressions.

Have you finished this lesson?

Introduction to SQL – All Lessons