CASE in SQL?๐ ๐ ๐ฒ: Use
CASE to add conditional logic inside SELECT, WHERE, or ORDER BY.Example:
SELECT name,
salary,
CASE
WHEN salary >= 80000 THEN 'High'
WHEN salary >= 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_level
FROM employees;
๐ง Logic Breakdown:
- Works like if-else
- Evaluates conditions top to bottom
- Returns the first match
-
ELSE is optional (defaults to NULL)โ Use Case:
- Create custom categories
- Replace values based on logic
- Conditional ordering or filtering
๐ฌ Tap โค๏ธ for more!