Beginner

377. CASE Expression

Learn how to create conditional logic inside SQL queries.

📘 Lesson 377 of 728 ⏱ 12 min 🎯 Intermediate

The CASE expression evaluates conditions and returns a result based on which condition is satisfied.

CASE allows analysts to transform raw database values into meaningful business categories directly inside SQL.

Classify employees into salary bands.

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 for business rules, segmentation, categorization, KPI calculations, conditional labels, and reporting.

  • CASE provides conditional logic.
  • It can return different values for different conditions.
  • WHEN defines a condition.
  • THEN defines the result.
  • ELSE handles remaining cases.
  • END closes the CASE expression.
What is CASE used for in SQL?

CASE is used to apply conditional logic and return different results based on specified conditions.

Is CASE only used in SELECT?

No. CASE expressions can be used in many SQL expressions, including SELECT, ORDER BY, GROUP BY, and conditional calculations.

Have you finished this lesson?

Introduction to SQL – All Lessons