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 FALSE, NULL if condition is 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

  • Condition must evaluate to BOOLEAN
  • true_value and false_value must be of compatible types
  • If condition is NULL, returns NULL
  • If false_value is omitted, returns NULL when condition is FALSE
  • Short-circuits evaluation: only evaluates the branch that will be returned

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 returns NULL
    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
12nullyesno100null

Last update at: 2026/03/03 16:47:38
Last updated: 2026-03-03 16:48:19