IF()

All functions > CONDITIONAL > IF()

Returns one value if a condition is true, another if false

Signatures

Returns: true_value if condition is TRUE, false_value if condition is FALSE or NULL

IF(condition: BOOLEAN, true_value: T, [false_value: T]) → T
sql
ParameterTypeRequiredDescription
conditionBOOLEANYesBoolean expression to evaluate
true_valueTYesValue to return if condition is TRUE
false_valueTNoValue to return if condition is FALSE (optional, defaults to NULL)

Notes

  • The condition must be BOOLEAN (use NULL::BOOLEAN when you need a typed unknown condition).
  • true_value and false_value must be type-compatible.
  • If the condition is NULL, it falls through to the else branch (returns false_value, or NULL if omitted). This matches CASE WHEN behavior.
  • If false_value is omitted, the result is NULL when the condition is FALSE or NULL.
  • Short-circuits: only the selected branch is evaluated.

Examples

FeatureQL
SELECT
    f1 := IF(TRUE, 1, 2), -- Condition is TRUE, returns first value
    f2 := IF(FALSE, 1, 2), -- Condition is FALSE, returns second value
    f3 := IF(NULL::BOOLEAN, 1, 2), -- NULL condition falls through to else branch
    f4 := IF(5 > 3, 'yes', 'no'), -- Expression evaluation TRUE
    f5 := IF(5 < 3, 'yes', 'no'), -- Expression evaluation FALSE
    f6 := IF(TRUE, 100), -- Omit false_value, Returns TRUE_value when TRUE
    f7 := IF(FALSE, 100) -- Omit false_value, returns NULL when FALSE
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 VARCHARf5 VARCHARf6 BIGINTf7 BIGINT
122yesno100null

Last update at: 2026/05/26 17:22:09