Skip to main content
Version: 24.3.x

Categories: Conditional

CASE

Evaluates a list of conditions and returns the first resulting true expression. If a true expression is not found, will return the ELSE statement, if present, or else will return NULL.

Syntax

CASE expression WHEN condition THEN result [ ...n ] [ ELSE result ] END → same as input type

  • expression: A valid SQL expression, typically, a column name.
  • condition: A boolean expression. If condition is true, then return result.
  • result: A valid SQL expression.

Examples

CASE example
SELECT
CASE categories
WHEN 'Restaurants' THEN 'food'
WHEN 'Hotels' THEN 'travel'
ELSE 'no result'
END
FROM table-name
-- food

CASE WHEN condition THEN result [ ...n ] [ ELSE result ] END → same as input type

  • condition: A boolean expression. If expression=condition is true, then return result.
  • result: A valid SQL expression.

Examples

CASE example
SELECT
CASE
WHEN categories='Restaurants' THEN 'food'
WHEN categories='Hotels' THEN 'travel'
ELSE 'no result'
END
FROM table-name
-- food