Control Flow Functions (IF, IFNULL, COALESCE)
Control Flow Functions: IF, IFNULL, and COALESCE
Relational databases are not limited to passive data retrieval. You often need inline conditional logic: classifying orders as High Value vs Standard, substituting missing telephone numbers, or preventing division by zero errors. MySQL provides several compact Control Flow Functions: IF(), IFNULL(), NULLIF(), and the ANSI standard COALESCE().
1. The Ternary IF(condition, true_val, false_val) Function
MySQL's built-in IF() function operates exactly like the ternary operator in programming languages (condition ? true_val : false_val):
2. Replacing NULLs: IFNULL(expression, fallback)
The IFNULL() function takes two arguments:
- 1If the first argument is NOT NULL, it returns the first argument.
- 2If the first argument IS NULL, it returns the second argument (the fallback).
3. The Universal Standard: COALESCE(val1, val2, ..., valN)
While IFNULL() is a MySQL-specific convenience limited to two arguments, COALESCE() is the ANSI SQL standard function supported by every relational database on Earth. It evaluates arguments from left to right and returns the first non-null value:
4. Preventing Errors: NULLIF(val1, val2)
The NULLIF() function compares two arguments:
- If
val1 = val2, it returnsNULL! - Otherwise, it returns
val1.
The Classic Use Case: Safe Division by Zero Prevention
5. Comparison: Control Flow Functions
| Function | Standard? | Arguments | Return Rule |
|---|---|---|---|
IF(cond, a, b) | MySQL Only | Exactly 3 | Returns a if cond is true; else b. |
IFNULL(a, b) | MySQL Only | Exactly 2 | Returns a if a is not null; else b. |
COALESCE(a, b, ...) | ANSI SQL | 2 or more | Returns the first non-null argument in list. |
NULLIF(a, b) | ANSI SQL | Exactly 2 | Returns NULL if a = b; else a. |
6. Best Practices & Common Pitfalls
- Prefer COALESCE over IFNULL for Portability: If there is any chance your application might migrate to PostgreSQL, Oracle, or SQLite, always choose
COALESCE()overIFNULL(). - Complex Multi-Branch Logic: For multi-branch conditional trees with more than two outcomes, avoid nesting multiple
IF()statements. Use standardCASE WHEN ... THEN ... ELSE ... ENDexpressions instead.
Multiple Choice Questions
1. What does SELECT IF(10 > 5, 'Yes', 'No'); return in MySQL?
A. Yes B. No C. TRUE D. NULL Answer: A Explanation: The IF() function evaluates the boolean condition 10 > 5 to TRUE, returning the second argument ('Yes').
2. What will COALESCE(NULL, NULL, 'Found Me', 'Backup') return?
A. NULL B. 'Found Me' C. 'Backup' D. 0 Answer: B Explanation: COALESCE() evaluates arguments in order and returns the first non-null entry ('Found Me').
3. What does NULLIF(50, 50) return?
A. 50 B. 0 C. NULL D. TRUE Answer: C Explanation: NULLIF(a, b) returns NULL whenever its two arguments are equal to each other.
4. Which function is an official ANSI SQL standard supported across all major database engines?
A. IF() B. IFNULL() C. COALESCE() D. NVL() Answer: C Explanation: COALESCE() and NULLIF() are part of the core ANSI SQL standard, whereas IF() and IFNULL() are MySQL-specific functions.
5. How can NULLIF be used to prevent division by zero errors when dividing revenue by visits?
A. revenue / ZERO(visits) B. revenue / NULLIF(visits, 0) C. NULLIF(revenue / visits, 0) D. DIVIDE(revenue, visits) Answer: B Explanation: NULLIF(visits, 0) returns NULL when visits = 0. Dividing by NULL evaluates gracefully to NULL instead of crashing.
Aggregate Functions: COUNT, SUM, AVG, MIN, MAX
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Date & Time Functions | Aggregate Functions: COUNT, SUM, AVG, MIN, MAX |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.