Conditional functions and expressions
Functions that return one of their arguments by evaluating in an if-else manner.
CASE WHEN condition THEN result [WHEN ...] [ELSE default_result]ENDInput:
One or multiple *WHENcondition THENresult clauses are used and the expression can optionally have an *ELSEdefault_result clause. Everycondition should be a boolean expression.
Output: one of theresult expressions if the corresponding *WHENcondition evaluates totrue or thedefault_result if all *WHENcondition clauses evaluate tofalse. If the optional *ELSEdefault_result clause is missing and all *WHENcondition clauses evaluate tofalse thennull is returned.
Description: The CASE expression is a generic conditional expression which simulates if/else statements of other programming languages If the condition’s result is true, the value of the result expression that follows the condition will be the returned the subsequent when clauses will be skipped and not processed.
SELECT CASE WHEN 1 > 2 THEN 'elastic' WHEN 2 <= 3 THEN 'search' END AS "case"; case---------------searchSELECT CASE WHEN 1 > 2 THEN 'elastic' WHEN 2 > 10 THEN 'search' END AS "case"; case---------------nullSELECT CASE WHEN 1 > 2 THEN 'elastic' WHEN 2 > 10 THEN 'search' ELSE 'default' END AS "case"; case---------------defaultAs a variant, a case expression can be expressed with a syntax similar toswitch-case of other programming languages:
CASE expression WHEN value1 THEN result1 [WHEN value2 THEN result2] [WHEN ...] [ELSE default_result]ENDIn this case it’s transformed internally to:
CASE WHEN expression = value1 THEN result1 [WHEN expression = value2 THEN result2] [WHEN ...] [ELSE default_result]ENDSELECT CASE 5 WHEN 1 THEN 'elastic' WHEN 2 THEN 'search' WHEN 5 THEN 'elasticsearch' END AS "case"; case---------------elasticsearchSELECT CASE 5 WHEN 1 THEN 'elastic' WHEN 2 THEN 'search' WHEN 3 THEN 'elasticsearch' ELSE 'default' END AS "case"; case---------------defaultAll result expressions must be of compatible data types. More specifically all result expressions should have a compatible data type with the 1stnon-null result expression. E.g.:
for the following query:
CASE WHEN a = 1 THEN null WHEN a > 2 THEN 10 WHEN a > 5 THEN 'foo'ENDan error message would be returned, mentioning thatfoo is of data typekeyword, which does not match the expected data typeinteger (based on result10).
CASE can be used as a GROUP BY key in a query to facilitate custom bucketing and assign descriptive names to those buckets. If, for example, the values for a key are too many or, simply, ranges of those values are more interesting than every single value, CASE can create custom buckets as in the following example:
SELECT count(*) AS count, CASE WHEN NVL(languages, 0) = 0 THEN 'zero' WHEN languages = 1 THEN 'one' WHEN languages = 2 THEN 'bilingual' WHEN languages = 3 THEN 'trilingual' ELSE 'multilingual' END as lang_skillsFROM employeesGROUP BY lang_skillsORDER BY lang_skills;With this query, one can create normal grouping buckets for values0, 1, 2, 3 with descriptive names, and every value>= 4 falls into themultilingual bucket.
COALESCE( expression, expression, ...)Input:
- 1st expression
- 2nd expression
…
Nth expression
COALESCE can take an arbitrary number of arguments.
Output: one of the expressions ornull
Description: Returns the first of its arguments that is not null. If all arguments are null, then it returnsnull.
SELECT COALESCE(null, 'elastic', 'search') AS "coalesce"; coalesce---------------elasticSELECT COALESCE(null, null, null, null) AS "coalesce"; coalesce---------------nullGREATEST( expression, expression, ...)Input:
- 1st expression
- 2nd expression
…
Nth expression
GREATEST can take an arbitrary number of arguments and all of them must be of the same data type.
Output: one of the expressions ornull
Description: Returns the argument that has the largest value which is not null. If all arguments are null, then it returnsnull.
SELECT GREATEST(null, 1, 2) AS "greatest"; greatest---------------2SELECT GREATEST(null, null, null, null) AS "greatest"; greatest---------------nullIFNULL( expression, expression)Input:
- 1st expression
- 2nd expression
Output: 2nd expression if 1st expression is null, otherwise 1st expression.
Description: Variant ofCOALESCE with only two arguments. Returns the first of its arguments that is not null. If all arguments are null, then it returnsnull.
SELECT IFNULL('elastic', null) AS "ifnull"; ifnull---------------elasticSELECT IFNULL(null, 'search') AS "ifnull"; ifnull---------------searchIIF( expression, expression, [expression])Input:
- boolean condition to check
- return value if the boolean condition evaluates to
true - return value if the boolean condition evaluates
false; optional
Output: 2nd expression if 1st expression (condition) evaluates totrue. If it evaluates tofalse return 3rd expression. If 3rd expression is not provided returnnull.
Description: Conditional function that implements the standardIF <condition> THEN <result1> ELSE <result2> logic of programming languages. If the 3rd expression is not provided and the condition evaluates tofalse,null is returned.
SELECT IIF(1 < 2, 'TRUE', 'FALSE') AS result1, IIF(1 > 2, 'TRUE', 'FALSE') AS result2; result1 | result2---------------+---------------TRUE |FALSESELECT IIF(1 < 2, 'TRUE') AS result1, IIF(1 > 2 , 'TRUE') AS result2; result1 | result2---------------+---------------TRUE |nullIIF functions can be combined to implement more complex logic simulating theCASE expression. E.g.:
IIF(a = 1, 'one', IIF(a = 2, 'two', IIF(a = 3, 'three', 'many')))ISNULL( expression, expression)Input:
- 1st expression
- 2nd expression
Output: 2nd expression if 1st expression is null, otherwise 1st expression.
Description: Variant ofCOALESCE with only two arguments. Returns the first of its arguments that is not null. If all arguments are null, then it returnsnull.
SELECT ISNULL('elastic', null) AS "isnull"; isnull---------------elasticSELECT ISNULL(null, 'search') AS "isnull"; isnull---------------searchLEAST( expression, expression, ...)Input:
- 1st expression
- 2nd expression
…
Nth expression
LEAST can take an arbitrary number of arguments and all of them must be of the same data type.
Output: one of the expressions ornull
Description: Returns the argument that has the smallest value which is not null. If all arguments are null, then it returnsnull.
SELECT LEAST(null, 2, 1) AS "least"; least---------------1SELECT LEAST(null, null, null, null) AS "least"; least---------------nullNULLIF( expression, expression)Input:
- 1st expression
- 2nd expression
Output:null if the 2 expressions are equal, otherwise the 1st expression.
Description: Returnsnull when the two input expressions are equal and if not, it returns the 1st expression.
SELECT NULLIF('elastic', 'search') AS "nullif"; nullif---------------elasticSELECT NULLIF('elastic', 'elastic') AS "nullif"; nullif:s---------------nullNVL( expression, expression)Input:
- 1st expression
- 2nd expression
Output: 2nd expression if 1st expression is null, otherwise 1st expression.
Description: Variant ofCOALESCE with only two arguments. Returns the first of its arguments that is not null. If all arguments are null, then it returnsnull.
SELECT NVL('elastic', null) AS "nvl"; nvl---------------elasticSELECT NVL(null, 'search') AS "nvl"; nvl---------------search