The COALESCE function evaluates expressions from left to right and returns the first value that is not NULL.
Real-world databases often contain missing values. COALESCE allows analysts to provide fallback values and make query results easier to interpret.
Display a customer's phone number and use an alternate phone number when the primary number is missing.
SELECT
CustomerName,
COALESCE(Phone, AlternatePhone, 'Not Available') AS ContactNumber
FROM Customers;
COALESCE is widely used for data cleaning, reporting, fallback values, and handling missing business information.
- COALESCE handles NULL values.
- It checks expressions from left to right.
- It returns the first non-NULL value.
- It can accept multiple expressions.
- It is useful for fallback values.
What does COALESCE do?
COALESCE returns the first non-NULL value from a list of expressions.
Why is COALESCE useful?
It helps handle missing values and provides fallback values when the preferred value is NULL.