MariaDB Control Flow Functions — IF, CASE, COALESCE
MariaDB
Complete reference for MariaDB control flow functions covering IF, CASE, COALESCE, IFNULL, NULLIF, NVL, NVL2, and DECODE_ORACLE. Every function includes syntax, NULL-handling examples, and comparisons with MySQL and PostgreSQL conditional expressions. Updated for MariaDB 11.x.
What are MariaDB Control Flow Functions — IF, CASE, COALESCE?
MariaDB control flow functions handle conditional branching and NULL substitution in SQL queries. COALESCE() returns the first non-NULL argument, IF(condition, true_val, false_val) is a shorthand ternary, and IFNULL() is the two-argument NULL coalescing shortcut. MariaDB adds Oracle compatibility functions NVL() and NVL2() in SQL_MODE=ORACLE, and DECODE_ORACLE() provides Oracle-style DECODE(). These functions differ from PostgreSQL's COALESCE and CASE in minor syntax details but are conceptually equivalent.
IF
→ ANY (type of true_value or false_value)
Returns true_value if condition is true (non-zero and non-NULL), otherwise returns false_value.
Signature
IF(condition, true_value, false_value)
Parameters
Parameter
Type
Description
condition
BOOLEAN / numeric
Expression evaluated as true (non-zero, non-NULL) or false
Returns expr if it is not NULL; otherwise returns fallback. Equivalent to COALESCE(expr, fallback) with exactly two arguments.
Signature
IFNULL(expr, fallback)
Parameters
Parameter
Type
Description
expr
ANY
Expression to test for NULL
fallback
ANY
Value returned when expr is NULL
Examples
NULL replaced by fallback
sql
SELECTIFNULL(NULL, 'default');
→-- default
Non-NULL returned as-is
sql
SELECTIFNULL('value', 'default');
→-- value
Replace NULL column
sql
SELECTIFNULL(middle_name, '') FROMcustomers;
→-- middle name or empty string
0 is not NULL
sql
SELECTIFNULL(0, 99);
→-- 0
Column fallback
sql
SELECTIFNULL(price, list_price) FROMproducts;
→-- price or list_price
⚠Anti-Pattern— Using IFNULL on aggregates without considering GROUP BY
SELECT IFNULL(SUM(amount), 0) looks safe, but SUM() over an empty set already returns NULL — make sure you actually need the IFNULL wrapper, and be aware it does not change row counts.
✓ Instead: Use COALESCE(SUM(amount), 0) which is more portable and equally clear.
⇄ vs PostgreSQL: PostgreSQL has no IFNULL(); use COALESCE(expr, fallback).
Conditional expression with two forms: simple CASE compares an expression against fixed values; searched CASE evaluates arbitrary boolean conditions. Returns the value of the first matching THEN clause, or the ELSE value (NULL if ELSE is omitted).
Signatures
CASE expr WHEN val1 THEN result1 [WHEN val2 THEN result2 ...] [ELSE default] END
CASE WHEN condition1 THEN result1 [WHEN condition2 THEN result2 ...] [ELSE default] END
Parameters
Parameter
Type
Description
expr
ANY
Expression compared against WHEN values (simple form only)
condition
BOOLEAN
Boolean expression evaluated for each WHEN clause (searched form)
result
ANY
Value returned for a matching WHEN branch
default
ANY
Value returned when no WHEN matches; NULL if ELSE is omitted
MariaDB 10.3.2+→ ANY (type of true_value or false_value)
Returns true_value if condition is true (non-zero and non-NULL), otherwise returns false_value. IIF is an alias for IF() introduced in MariaDB 10.3.2 for compatibility with Microsoft SQL Server and Access.
Signature
IIF(condition, true_value, false_value)
Parameters
Parameter
Type
Description
condition
BOOLEAN / numeric
Expression evaluated as true (non-zero, non-NULL) or false
true_value
ANY
Value returned when condition is true
false_value
ANY
Value returned when condition is false or NULL
Examples
Identical to IF(1>0,'yes','no')
sql
SELECTIIF(1>0, 'yes', 'no');
→-- yes
Column-based branch
sql
SELECTIIF(stock>0, 'In Stock', 'Out of Stock') FROMproducts;
MariaDB 10.3+→ ANY (type of not_null_value or null_value)
Returns not_null_value if expr is not NULL; returns null_value if expr is NULL. An Oracle compatibility function added in MariaDB 10.3. Extends NVL by allowing a different return value for both the NULL and non-NULL cases.
⚠Anti-Pattern— Expecting NVL2 argument order to match IF()
IF(condition, true_val, false_val) and NVL2(expr, not_null_val, null_val) share the same argument order (true branch second, false branch third), but NVL2's first argument is an expression tested for NULL, not a boolean. Passing a boolean condition to NVL2 evaluates whether that expression is NULL or not — usually not what you want.
✓ Instead: Use IF(expr IS NULL, null_val, not_null_val) or CASE WHEN to make the NULL check explicit.
◆MariaDB Note
Oracle compatibility function available in MariaDB 10.3+.
⇄ vs MySQL: NVL2() does not exist in MySQL.
⇄ vs PostgreSQL: PostgreSQL has no NVL2(); use CASE WHEN expr IS NOT NULL THEN a ELSE b END.
MariaDB 10.3+ (Oracle mode)→ ANY (type of matched return value or default)
Compares expr against a series of search values and returns the corresponding result for the first match. An optional final argument serves as the default when no match is found. Available in MariaDB 10.3+ under Oracle SQL mode. NULL-safe: NULL = NULL is treated as a match.
⚠Anti-Pattern— Confusing MariaDB DECODE with MySQL DECODE
MySQL has a DECODE(ciphertext, key) function that decrypts DES-encrypted data — completely different from Oracle/MariaDB DECODE. Using Oracle-style DECODE in MySQL will produce wrong results silently.
✓ Instead: In MySQL, use CASE WHEN. In MariaDB with Oracle mode, DECODE(expr, ...) works as the Oracle-compatible conditional.
◆MariaDB Note
Requires sql_mode=ORACLE or the Oracle compatibility plugin in MariaDB 10.3+.
⇄ vs MySQL: MySQL DECODE(ciphertext, key) is a DES decryption function — completely different. The Oracle-compatible conditional DECODE does not exist in MySQL.
⇄ vs PostgreSQL: PostgreSQL has no DECODE() conditional; use CASE WHEN. (PostgreSQL does have decode() for binary encoding, unrelated.)