Question №24
Remaining:
How to use the CASE operator in SQL?
Sample Answer
Show Answer by Default
The CASE operator is used to implement conditional logic in SQL queries. It allows you to return values based on conditions, similar to the IF-ELSE statement.
MySQL 8.1CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END
Examples
- Assigning categories based on salary:
MySQL 8.1
SELECT name, salary, CASE WHEN salary >= 80000 THEN 'High' WHEN salary >= 50000 THEN 'Medium' ELSE 'Low' END AS salary_category FROM employees;