MariaDB IF(), IFNULL(), and NULLIF(): When to Use Each

In MariaDB, IF() chooses between two results, IFNULL() supplies a fallback for a missing value, and NULLIF() returns NULL when its two arguments compare equal. Choose the function according to the result your query needs.

Function When to use it Example
IF() Choose based on a condition Label a quantity
IFNULL() Replace a NULL first value Display a fallback name
NULLIF() Return NULL for a matching value Convert a zero denominator to NULL

Choose a value with IF()

IF() returns its second argument when the condition is true and its third argument otherwise. Connect to MariaDB and run this query. It requires no table.

SELECT IF(3 > 2, 'T', 'F') AS true_condition,
       IF(3 < 2, 'T', 'F') AS false_condition,
       IF(NULL, 'T', 'F') AS unknown_condition,
       IF(0, 'T', 'F') AS zero_condition,
       IF(-2, 'T', 'F') AS nonzero_condition;

The results are T, F, F, F, T. Zero and NULL both reach the third argument. Test IS NULL separately when an unknown quantity needs a different label from zero. Conditions expressed as strings can be converted to numbers, so use an explicit comparison such as quantity > 0. Stored programs use a separate IF … THEN … END IF statement to branch across statements.

Supply a fallback with IFNULL()

IFNULL() returns its fallback only if the first value is NULL. The example wraps the empty string in brackets so it remains visible.

SELECT IFNULL(NULL, 'unlisted') AS missing_value,
       IFNULL('Alpha', 'unlisted') AS present_value,
       CONCAT('[', IFNULL('', 'unlisted'), ']') AS empty_value,
       IFNULL(0, 99) AS zero_value,
       IFNULL(NULL, NULL) AS both_null;

In a SQL mode that preserves empty strings, the results are unlisted, Alpha, [], 0, NULL. Both zero and the empty string remain as values. Test for missing data with IS NULL. To choose the first known value from several candidates, use COALESCE().

Turn an equal value into NULL with NULLIF()

NULLIF() returns NULL if the = comparison is true. Otherwise it returns its first value. This lets you turn a zero denominator into NULL before dividing.

SELECT NULLIF(5, 5) AS equal_values,
       NULLIF(5, 2) AS different_values,
       NULLIF(NULL, 5) AS null_first,
       NULLIF(5, NULL) AS null_second,
       NULLIF(NULL, NULL) AS two_nulls;

SELECT 12 / NULLIF(0, 0) AS zero_denominator,
       CAST(12 / NULLIF(3, 0) AS DECIMAL(7,1)) AS valid_denominator;

The first query returns NULL, 5, NULL, 5, NULL. Since 5 = NULL is unknown, NULLIF(5, NULL) keeps 5. The division query returns NULL and 4.0, with the numeric output set to one decimal place. Use zero for an undefined rate only if the reporting rules treat the two as equivalent.

Build names and completion rates in a report

The common table expression (CTE) supplies sample rows within the query. Trim the nickname and display unlisted when it is blank or missing. Label a missing completion count unknown before testing the threshold. Leave the rate NULL if the total is zero.

WITH sample_rows AS (
    SELECT 1 AS id, 'Alpha' AS nickname, 8 AS completed, 10 AS total
    UNION ALL SELECT 2, '', 0, 0
    UNION ALL SELECT 3, NULL, NULL, 5
)
SELECT id,
       IFNULL(NULLIF(TRIM(nickname), ''), 'unlisted') AS display_name,
       CASE WHEN completed IS NULL THEN 'unknown'
            ELSE IF(completed >= 5, 'met', 'review')
       END AS progress_label,
       CAST(100.0 * completed / NULLIF(total, 0) AS DECIMAL(7,1)) AS percent_value
FROM sample_rows
ORDER BY id;

WITH sample_counts AS (
    SELECT 8 AS completed
    UNION ALL SELECT 0
    UNION ALL SELECT NULL
)
SELECT COUNT(*) AS all_rows,
       COUNT(completed) AS known_rows,
       ROUND(AVG(completed), 2) AS known_average,
       ROUND(AVG(IFNULL(completed, 0)), 2) AS null_as_zero_average
FROM sample_counts;

The rows return Alpha, met, 80.0, unlisted, review, NULL, and unlisted, unknown, NULL. The second query counts 3 rows, 2 known counts, a known-value average of 4.00, and an average of 2.67 after replacement with zero. AVG() excludes NULL values and includes the replacement zeros.

Use CASE if you need several branches in a specific order. Decide separately how to store, calculate, and display missing values. Trimming names and showing unlisted are display choices in this example.