Skip to main content
CASE ... WHEN ...

Evaluates conditions and returns a value based on the first true condition

Updated over a week ago

Description

The CASE...WHEN... statement evaluates multiple conditions and returns a specific value when a condition is met.

Syntax

CASE
โ€‹WHEN condition1 THEN result1

WHEN condition 2 THEN result2

ELSE default_result

END

Output

text

Variables

  1. condition1, condition2, ...: Conditions to evaluate.

  2. result1, result2, ...: Values returned if the corresponding condition is true.

  3. default_result: Value returned if none of the conditions are true (optional).

Example

Consider a dataset with a column "Price":

Product

Price

Laptop

1200

Smartphone

800

Headphones

150

Camera

900

Using the CASE...WHEN... statement:

CASE 
WHEN Price > 1000 THEN 'High-end'
WHEN Price <= 1000 AND Price > 200 THEN 'Mid-range'
ELSE 'Budget'
END

Product

Price

Price Category

Laptop

1200

High-end

Smartphone

800

Mid-range

Headphones

150

Budget

Camera

900

Mid-range

Did this answer your question?