The CASE expression evaluates conditions and returns different results depending on which condition is satisfied.
CASE allows analysts to create business rules, categories, labels, and conditional calculations directly inside SQL queries.
Classify employees into salary bands based on their salary.
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 classification, risk categories, performance bands, conditional calculations, and reporting logic.
- CASE provides conditional logic.
- WHEN defines a condition.
- THEN defines the returned result.
- ELSE provides a fallback result.
- CASE can be used in SELECT, WHERE, ORDER BY, and other expressions.
What is CASE in SQL?
CASE is a conditional expression used to return different results based on specified conditions.
Where can CASE be used?
CASE can be used in SELECT statements and in many other SQL expressions such as ORDER BY and conditional calculations.