On this page

    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

    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

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