Beginner

568. JOIN with COALESCE

Learn how COALESCE can provide fallback values when JOIN results contain NULLs.

📘 Lesson 568 of 728 ⏱ 13 min 🎯 Intermediate

COALESCE returns the first non-NULL value from a list of expressions. It is often used after OUTER JOINs to replace missing related values with a meaningful default.

Reports often need readable values instead of NULL when a related record does not exist.

Show 'No Order' when a customer has no matching order.

SQL
SELECT
    c.CustomerName,
    COALESCE(CAST(o.OrderID AS VARCHAR(20)), 'No Order') AS OrderStatus
FROM Customers c
LEFT JOIN Orders o
    ON c.CustomerID = o.CustomerID;

Used in reporting, dashboards, data-quality handling, and customer analysis.

  • COALESCE returns the first non-NULL value.
  • It is useful with LEFT JOIN.
  • It can replace missing values with defaults.
  • Multiple fallback expressions can be supplied.
  • Data types should be compatible.
Why use COALESCE with LEFT JOIN?

COALESCE can replace NULL values from unmatched rows with a meaningful fallback value.

What does COALESCE return?

It returns the first non-NULL expression from the supplied list.

Have you finished this lesson?

Introduction to SQL – All Lessons