📊 Conditional Functions in SQL
Last Updated: January 2026
The CONDITIONAL FUNCTION is a function that returns a value based on a condition.
Most Common Conditional Functions:
IF()
Returns a value based on a condition.
/*
IF(condition, value_if_true, value_if_false)
*/
SELECT IF(10 > 5, 'Greater', 'Lesser');
-- Output: Greater
CASE Statement
Returns a value based on multiple conditions.
/*
CASE
WHEN condition1 THEN value1
WHEN condition2 THEN value2
...
ELSE value_default
END
*/
SELECT CASE
WHEN 10 > 5 THEN 'Greater'
WHEN 10 < 5 THEN 'Lesser'
ELSE 'Equal'
END;
-- Output: Greater
IFNULL()
Replace NULL values with another value.
/*
IFNULL(column_name, condition, value_if_null)
*/
SELECT IFNULL(NULL, 10); -- Output: 10
SELECT IFNULL(5, 10); -- Output: 5
NULLIF()
Returns the first argument if it is not equal to the second argument, otherwise returns NULL.
SELECT NULLIF(10, 10); -- Output: NULL
SELECT NULLIF(10, 5); -- Output: 10
COALESCE()
Returns the first non-NULL argument.
SELECT COALESCE(NULL, NULL, 10); -- Output: 10
SELECT COALESCE(NULL, 5, 10); -- Output: 5