For AI agents: the complete documentation index is at llms.txt. Every page is also available as markdown by appending .md to its URL, or by sending an Accept: text/markdown request header.

CASE keyword

Syntax​

CASE
WHEN condition THEN value
[WHEN condition THEN value ...]
[ELSE value]
END

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​

Tag each trade as bullish or bearish based on its side, using ELSE as the fallback:

CASE with ELSEDemo this query
SELECT symbol, side,
CASE
WHEN side = 'buy' THEN 'bullish'
ELSE 'bearish'
END AS sentiment
FROM trades
LIMIT -40;
symbolsidesentiment
BTC-USDbuybullish
ETH-USDsellbearish
BTC-USDbuybullish
SOL-USDsellbearish

Without ELSE, unmatched rows produce null:

CASE without ELSEDemo this query
SELECT symbol, side,
CASE
WHEN side = 'buy' THEN 'bullish'
END AS sentiment
FROM trades
LIMIT -40;
symbolsidesentiment
BTC-USDbuybullish
ETH-USDsellnull
BTC-USDbuybullish
SOL-USDsellnull