πΉ What is SQL CASE?
The CASE statement in SQL is like an IF-THEN-ELSE statement in programming.
It allows you to:
Think of it like this:
βIf this condition is true β return this value
If another condition is true β return that value
Otherwise β return something elseβ
πΉ Why Use SQL CASE?
You use CASE when you want to:
πΉ Types of CASE in SQL
There are two types of CASE:
| Type | Description | |-----------------|------------| | Simple CASE | Compares one column to multiple values | | Searched CASE | Uses full conditions (>, <, =, etc.) |
πΉ Sample Employees Table
Weβll use this table for examples:
| id | name | department | salary | |----|---------|------------|--------| | 1 | Alice | HR | 50000 | | 2 | Bob | Sales | 60000 | | 3 | Charlie | IT | 55000 | | 4 | David | HR | 45000 | | 5 | Eve | Sales | 70000 |
1οΈβ£ Simple CASE
Used when you compare one column to specific values.
Syntax:
CASE column_name
WHEN value1 THEN result1
WHEN value2 THEN result2
ELSE result
END
Example: Department Labels
SELECT name,
department,
CASE department
WHEN 'HR' THEN 'Human Resources'
WHEN 'IT' THEN 'Information Technology'
WHEN 'Sales' THEN 'Sales Department'
ELSE 'Other'
END AS department_full_name
FROM Employees;
What Happens?
2οΈβ£ Searched CASE
Used when you need conditions (>, <, >=, etc.)
Syntax:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result
END
Example: Salary Categories
SELECT name,
salary,
CASE
WHEN salary >= 65000 THEN 'High Salary'
WHEN salary >= 50000 THEN 'Medium Salary'
ELSE 'Low Salary'
END AS salary_category
FROM Employees;
Explanation:
SQL checks from top to bottom.
πΉ Using CASE in ORDER BY
You can even use CASE to control sorting.
SELECT name, department
FROM Employees
ORDER BY
CASE
WHEN department = 'HR' THEN 1
WHEN department = 'IT' THEN 2
ELSE 3
END;
This sorts:
πΉ Using CASE with Aggregate Functions
Example: Count employees by salary level.
SELECT
CASE
WHEN salary >= 60000 THEN 'High'
ELSE 'Standard'
END AS salary_group,
COUNT(*) AS total_employees
FROM Employees
GROUP BY
CASE
WHEN salary >= 60000 THEN 'High'
ELSE 'Standard'
END;
πΉ Important Rules
| Rule | Explanation | |------|------------| | CASE must end with END | END is mandatory | | ELSE is optional | But recommended | | Order matters | First true condition is executed | | Works inside SELECT, ORDER BY, GROUP BY | Very flexible |
πΉ Visual Explanation
Think of CASE like this:
IF salary >= 65000 β High ELSE IF salary >= 50000 β Medium ELSE β Low
Itβs just SQLβs way of writing IF-ELSE logic.
πΉ When Should You Use CASE?
Use CASE when:
-- Simple CASE Example
SELECT name,
department,
CASE department
WHEN 'HR' THEN 'Human Resources'
WHEN 'IT' THEN 'Information Technology'
WHEN 'Sales' THEN 'Sales Department'
ELSE 'Other'
END AS department_full_name
FROM Employees;
-- Searched CASE Example
SELECT name,
salary,
CASE
WHEN salary >= 65000 THEN 'High Salary'
WHEN salary >= 50000 THEN 'Medium Salary'
ELSE 'Low Salary'
END AS salary_category
FROM Employees;
-- CASE in ORDER BY
SELECT name, department
FROM Employees
ORDER BY
CASE
WHEN department = 'HR' THEN 1
WHEN department = 'IT' THEN 2
ELSE 3
END;
-- CASE with GROUP BY
SELECT
CASE
WHEN salary >= 60000 THEN 'High'
ELSE 'Standard'
END AS salary_group,
COUNT(*) AS total_employees
FROM Employees
GROUP BY
CASE
WHEN salary >= 60000 THEN 'High'
ELSE 'Standard'
END;