titlesidebar_labeldescription
CASE keyword
CASE
CASE SQL keyword reference documentation.

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
  1. SELECT
  2. name,
  3. CASE
  4. WHEN age > 18 THEN 'major'
  5. ELSE 'minor'
  6. END
  7. FROM my_table

Result

namecase
Tomminor
Jerrymajor
Annamajor
Jackminor
  1. SELECT
  2. name,
  3. CASE
  4. WHEN age > 18 THEN 'major'
  5. END
  6. FROM my_table

Result

namecase
Tomnull
Jerrymajor
Annamajor
Jacknull