-- Show display_name, fall back to email, fall back to "Anonymous"
SELECT id,
COALESCE(display_name, email, 'Anonymous') AS shown_name
FROM users;
-- NULLIF — divide by zero protection
SELECT amount, qty,
amount / NULLIF(qty, 0) AS unit_price -- returns NULL instead of error
FROM line_items;
-- Treat empty string as NULL (common normalization)
UPDATE users
SET email = NULLIF(TRIM(email), '');
-- Difference between COALESCE and ISNULL/IFNULL:
-- COALESCE — standard SQL, variadic, returns the type of the first non-null
-- IFNULL — MySQL, takes exactly 2 args
-- ISNULL — SQL Server, takes exactly 2 args
Create a free account and build your private vault. Share publicly whenever you want.