skip to main content
Expressions, conditions, and queries : Expressions : CASE : Searched CASE function
 

Searched CASE function

Syntax 

CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END

Examples

SELECT title, 'Movie Type' =
CASE
WHEN movie_description LIKE '%Horror%' THEN 'Horror'
WHEN movie_description LIKE '%Adventure%' THEN 'Adventure'
ELSE 'Family'
END
FROM movie_titles;
SELECT COUNT(empno), MAX(sal) FROM emp GROUP BY CASE WHEN deptno > 5 THEN deptno ELSE deptno + 10 END;
SELECT * FROM emp ORDER BY CASE WHEN MOD(deptno, 2)>=1 THEN 'Group 1' ELSE 'Group 2' END;
SELECT COUNT(*), deptno FROM emp GROUP BY deptno HAVING CASE WHEN MOD(deptno, 2)>=1 THEN MAX(sal) ELSE MAX(comm) END > AVG(sal);