Skip to content

PostgreSQL CASE Expression

The CASE expression in PostgreSQL is used to add conditional logic inside SQL queries. It works like an if-else statement:

  • WHEN condition THEN result → if condition is true, return result.
  • ELSE result → if none of the conditions are true, return this.

📝 Syntax

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

👉 You can use CASE in SELECT, WHERE, ORDER BY, and more.


✅ Example: Salary Categories

SELECT fname, salary,
CASE
WHEN salary >= 55000 THEN 'High'
WHEN salary BETWEEN 48000 AND 55000 THEN 'Mid'
ELSE 'Low'
END AS sal_cat
FROM employee;

Sample Data (employee table)

emp_idfnamesalary
1Raj60000
2Sita50000
3Amit45000

Result

fnamesalarysal_cat
Raj60000High
Sita50000Mid
Amit45000Low

🎯 Example: Pass/Fail Check

SELECT fname,
CASE
WHEN marks >= 40 THEN 'Pass'
ELSE 'Fail'
END AS result
FROM student;

👉 This checks student marks and labels them as Pass or Fail.


🔄 Example: Using CASE in ORDER BY

You can sort data with custom order:

SELECT fname, dept,
CASE
WHEN dept = 'HR' THEN 1
WHEN dept = 'IT' THEN 2
ELSE 3
END AS sort_order
FROM employee
ORDER BY sort_order;

✔ HR will come first, then IT, then others.


🧩 Nested CASE Example

You can also nest CASE inside CASE for complex rules:

SELECT fname, salary,
CASE
WHEN salary >= 55000 THEN
CASE
WHEN salary >= 70000 THEN 'Very High'
ELSE 'High'
END
ELSE 'Low or Mid'
END AS salary_band
FROM employee;

📋 Quick Reference (Cheat Sheet)

Usage ExamplePurpose
CASE WHEN salary >= 55000 THEN 'High' ELSE 'Low' ENDCategorize salary
CASE WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' ENDPass/Fail result
CASE WHEN dept='HR' THEN 1 WHEN dept='IT' THEN 2 ELSE 3 ENDCustom sort order
Nested CASEHandle complex conditions

✅ With CASE, you can make your queries smarter by adding decision-making directly inside SQL.