CASE keyword

Syntax#

Flow chart showing the syntax of CASE

Description#

CASE goes through a set of conditions and returns a value corresponding to the first condition met. Each new condition follows the WHEN condition THEN value syntax. The user can define a return value when no condition is met using ELSE. If ELSE is not defined and no conditions are met, then case returns null.

Examples#

Assume the following data

NameAge
Tom4
Jerry19
Anna25
Jack8
CASE with ELSE
select
Name,
case
when age > 18 then 'major'
else 'minor'
end
from table

Result

Namecase
Tomminor
Jerrymajor
Annamajor
Jackminor
CASE without ELSE
select
Name,
case
when age > 18 then 'major'
end
from table

Result

Namecase
Tomnull
Jerrymajor
Annamajor
Jacknull