The MySQL IF() function evaluates a condition and returns the second argument if true, else the third. It allows developers to perform deterministic data transformations directly within SQL queries.
EXPRESSIONNULL if any required input argument evaluates to NULL.condition, true_val, false_val-- Query demonstrating MySQL IF() usage in production queries
SELECT IF(stock > 0, 'In Stock', 'Out of Stock') AS status FROM products;
| status |
|---|
| In Stock |
IFNULL() or COALESCE() if your input columns allow NULL values.WHERE clauses disables B-tree index lookups (causing full table scans). Use generated columns with indexes when querying heavily.Write a SQL query using IF() to format or evaluate data retrieved from a database table.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With