Introduction
These functions help you replace missing or empty values with something useful, so your data looks complete and clear.
IFNULL(expression, replacement_value) COALESCE(expression1, expression2, ..., expressionN)
SELECT IFNULL(NULL, 'No data');
SELECT COALESCE(NULL, NULL, 'First non-null', 'Second');
SELECT IFNULL(5, 10);
SELECT COALESCE(NULL, 0, 1);
CREATE TABLE products ( id INT, name VARCHAR(20), price DECIMAL(5,2), discount DECIMAL(5,2) ); INSERT INTO products VALUES (1, 'Pen', 1.50, NULL), (2, 'Notebook', 3.00, 0.50), (3, 'Eraser', NULL, NULL); SELECT name, IFNULL(price, 0) AS price, IFNULL(discount, 0) AS discount, IFNULL(price, 0) - IFNULL(discount, 0) AS final_price, COALESCE(price, discount, 0) AS first_available_price FROM products;
IFNULL(expr1, expr2) function do in MySQL?COALESCE in MySQL?users with columns id, nickname, and fullname, what will this query return?SELECT id, COALESCE(nickname, fullname, 'Guest') AS display_name FROM users;
nickname = NULL and fullname = 'Alice'.SELECT IFNULL(name, 'Unknown' 'N/A') FROM employees;
email, then phone, and if both are NULL, show 'No contact'. Which query correctly uses COALESCE for this?Pass the test and this pattern moves unknown