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.
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.