Sometimes you need "IF/THEN" logic in your SQL. If a value is NULL, show "N/A". If one value equals another, return NULL. Control Flow Functions allow the database to make basic logic decisions as it processes each row.
Control Flow Functions: SQL functions that evaluate conditions and return different values based on those conditions. They allow for "procedural" logic inside a declarative query.
| Function | Description | Example | Result |
|---|---|---|---|
IF(expr, t, f) |
If expr is true, return t, else f |
IF(1>2, 'Yes', 'No') |
'No' |
IFNULL(val, default) |
Returns default if val is NULL |
IFNULL(NULL, 0) |
0 |
NULLIF(a, b) |
Returns NULL if a=b, else returns a |
NULLIF(5, 5) |
NULL |
COALESCE(a, b, ...) |
Returns the first non-NULL value | COALESCE(NULL, 5, 2) |
5 |
Data is messy.
IFNULLprevents apps from crashing when they try to display a NULL name or salary.NULLIFprevents "Division by Zero" errors by turning 0 into NULL before a divide.IFallows for quick categorization (e.g., classifying salary as "High" or "Low") without using the more verboseCASE.
A critical point for both IF and COALESCE is Short-Circuit Evaluation:
MySQL evaluates the arguments from left to right and stops as soon as it finds the answer.
SELECT COALESCE('Found', (SELECT complex_expensive_query));
-- MySQL will NEVER run the expensive subquery because the first argument is already non-NULL.This is a vital performance optimization for senior developers when dealing with multi-step fallback logic.
Example 1: The "N/A" Fallback
SELECT
First_Name,
IFNULL(Phone, 'No Phone Provided') AS Contact_Info
FROM Employees;Example 2: Preventing Division by Zero
-- If Sold_Units is 0, NULLIF turns it to NULL.
-- Any number / NULL = NULL (prevents crash).
SELECT
Total_Revenue / NULLIF(Sold_Units, 0) AS Price_Per_Unit
FROM Sales;Example 3: Hierarchical Fallback (COALESCE)
-- Find first available contact method
SELECT
First_Name,
COALESCE(Work_Phone, Mobile_Phone, Email, 'Untraceable') AS Preferred_Contact
FROM Employees;IFNULL(a, b)takes only two arguments. It is MySQL-specific.COALESCE(a, b, c, ...)takes unlimited arguments. It is ANSI SQL Standard.
Senior Tip: Always prefer COALESCE. It is more powerful and makes your code portable to other databases like PostgreSQL or SQL Server.
- Using
IFfor complex branching: WhileIF()is great for simple "one or the other" logic, it becomes unreadable when nested. For 3 or more conditions, useCASE(Topic 8.7). - Assuming
IFNULLworks on empty strings:IFNULL('', 'N/A')returns''(the empty string). To MySQL, an empty string is NOT NULL. UseNULLIFcombined withCOALESCEto handle this:COALESCE(NULLIF(col, ''), 'N/A').
Logical Mapping with IF:
-- Quickly flag status
SELECT
Username,
IF(is_active = 1, 'β
Active', 'β Deactivated') AS Status
FROM Users;- Task 1: Write a query that returns the
Cityof an employee, but if the city is NULL, return'Remote'. - Task 2: Use
COALESCEto return the first non-NULL value among columnsBonus,Commission, and0.