Menu

SQLite Flow Control Functions

SQLite conditional expressions return different values depending on whether an expression is true or a value is NULL. These functions are used inside SQL expressions; they do not control which SQL statements execute.

Function Use it when
coalesce() You need the first non-NULL value from two or more expressions.
ifnull() You need a fallback for one nullable expression.
iif() You want to select a result based on a condition.
nullif() You want to return NULL when two expressions compare equal.

Example: defaults, conditions, and sentinel values

SELECT
  coalesce(NULL, NULL, 'unknown') AS first_available,
  ifnull(NULL, 0) AS default_count,
  iif(7 > 5, 'high', 'low') AS category,
  nullif(0, 0) AS empty_if_zero;

The result is unknown, 0, high, and NULL, respectively. coalesce() and ifnull() handle missing values; nullif() can turn a special value into NULL; iif() selects a branch based on a condition.

Try the query in the SQLite Playground. For details on how SQLite stores NULL and other values, see storage classes and type affinity.

  1. coalesce

    The SQLite coalesce() function returns the first non-null value in the parameter list.
  2. ifnull

    Learn how SQLite ifnull(X, Y) replaces NULL values, how it differs from COALESCE, and why empty strings, zero and AVG need special care.
  3. iif

    The SQLite iif() function has 3 parameters, if the first parameter is true, it returns the second parameter, otherwise it returns the third parameter.
  4. nullif

    The SQLite nullif() function returns NULL if both parameters are the same, otherwise returns the first argument.