sqlbeginner

Handle NULLs with COALESCE

Use COALESCE to replace NULL values with a default in SQL queries.

sql
-- Replace NULL with a default value
SELECT
  employee_id,
  first_name,
  COALESCE(phone, 'N/A') AS phone,
  COALESCE(department, 'Unassigned') AS department
FROM employees;

-- Chain multiple fallbacks
SELECT
  product_id,
  COALESCE(sale_price, retail_price, wholesale_price, 0) AS effective_price
FROM products;

Use Cases

  • Replacing NULL values in reports
  • Default values for missing data

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.