IF() and IFNULL()
Result:
| name | stock | level |
|---|---|---|
| Laptop | 15 | OK |
| Phone | 30 | OK |
| Headphones | 100 | OK |
| Desk | 10 | OK |
| Chair | 25 | OK |
| Notebook | 500 | OK |
| Pen Pack | 300 | OK |
| Lamp | 0 | Out of stock |
IF(test, a, b) is MySQL-only shorthand. The portable form is CASE.
Result:
| name | contact | |
|---|---|---|
| Asha Rao | asha@example.com | asha@example.com |
| Ben Carter | ben@example.com | ben@example.com |
| Chen Wei | chen@example.com | chen@example.com |
| Dina Cohen | no email | Tel Aviv |
| Eli Brown | eli@example.com | eli@example.com |
| Fatima Khan | fatima@example.com | fatima@example.com |
CASE
Result:
| name | price | tier |
|---|---|---|
| Notebook | 3.50 | Budget |
| Pen Pack | 5.00 | Budget |
| Lamp | 45.00 | Mid |
| Headphones | 79.00 | Mid |
| Chair | 129.00 | Mid |
| Desk | 249.00 | Premium |
| Phone | 599.00 | Premium |
| Laptop | 899.00 | Premium |
NULLIF: Avoid Divide by Zero
Result:
| name | price_per_unit |
|---|---|
| Laptop | 59.93 |
| Phone | 19.97 |
| Headphones | 0.79 |
| Desk | 24.90 |
| Chair | 5.16 |
| Notebook | 0.01 |
| Pen Pack | 0.02 |
| Lamp | NULL |
Division by zero in MySQL returns NULL (with a warning) in normal queries and raises an error in strict INSERT or UPDATE. NULLIF makes the intent clear.
Conditional Aggregation
Result:
| customer_id | orders | shipped | pending | cancelled |
|---|---|---|---|---|
| 1 | 3 | 2 | 1 | 0 |
| 2 | 3 | 2 | 0 | 1 |
| 3 | 2 | 1 | 1 | 0 |
| 5 | 1 | 1 | 0 | 0 |
| 6 | 1 | 1 | 0 | 0 |
In MySQL a comparison is 1 or 0, so SUM(status = 'shipped') counts matches. In other databases write SUM(CASE WHEN ... THEN 1 ELSE 0 END).
Pivot: Rows to Columns
Result:
| category | under_100 | from_100 |
|---|---|---|
| Electronics | 1 | 2 |
| Furniture | 1 | 2 |
| Stationery | 2 | 0 |
MySQL has no PIVOT keyword. The column list is fixed in the query. For unknown columns, build the SQL text with GROUP_CONCAT and run it with a prepared statement.
Dynamic Pivot with a Prepared Statement
Result:
| customer_id | cancelled | pending | shipped |
|---|---|---|---|
| 1 | 0 | 1 | 2 |
| 2 | 1 | 0 | 2 |
| 3 | 0 | 1 | 1 |
| 5 | 0 | 0 | 1 |
| 6 | 0 | 0 | 1 |
Ordering with CASE and FIELD
Result:
| id | status |
|---|---|
| 4 | pending |
| 7 | pending |
| 1 | shipped |
| 2 | shipped |
| 3 | shipped |
| 6 | shipped |
| 8 | shipped |
| 9 | shipped |
| 10 | shipped |
| 5 | cancelled |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
CASE without ELSE | NULL for other rows | Add ELSE |
Using IFNULL with 3 arguments | Error | Use COALESCE |
Expecting PIVOT syntax | Syntax error | Use CASE with SUM |
| Dividing by a possible zero | NULL or error | Use NULLIF |
| Building SQL from user input | SQL injection | Use only trusted names |
Practice
- Label customers by order count with
CASE. - Make a report with one column per order status.
- Sort orders with custom status order.
- Show revenue per category as separate low and high price columns.
Conclusion
IF, IFNULL, COALESCE, NULLIF and CASE shape values. Conditional aggregation with SUM(CASE ...) creates pivot reports, since MySQL has no PIVOT.

