Beginner

328. COALESCE Function

Learn how to return the first non-NULL value from multiple expressions.

📘 Lesson 328 of 728 ⏱ 10 min 🎯 Intermediate

COALESCE evaluates expressions from left to right and returns the first value that is not NULL.

Real-world databases frequently contain missing values. COALESCE helps provide fallback values and create cleaner analytical results.

Display a customer's alternate phone number when the primary phone number is missing.

SQL
SELECT
    CustomerName,
    COALESCE(PrimaryPhone, AlternatePhone, 'Not Available') AS ContactNumber
FROM Customers;

COALESCE is widely used for NULL handling, reporting, data preparation, financial calculations, and fallback values.

  • COALESCE handles NULL values.
  • It evaluates expressions from left to right.
  • It returns the first non-NULL value.
  • Multiple fallback values can be provided.
  • It is widely used in reporting.
What does COALESCE do?

COALESCE returns the first non-NULL value from a list of expressions.

Why is COALESCE useful for Data Analysts?

It allows analysts to handle missing values and provide meaningful fallback values in reports and calculations.

Have you finished this lesson?

Introduction to SQL – All Lessons