← Back to Questions
SQL

What is the COALESCE function in SQL?

Learn What is the COALESCE function in SQL? with simple explanations, real-time examples, interview tips and practical use cases.

The COALESCE function in SQL is used to return the first non-NULL value from a list of expressions.

In simple words:

COALESCE helps replace NULL values with meaningful alternative values.


Why COALESCE is Important

Enterprise databases often contain:

  • Missing values
  • Incomplete records
  • NULL data

COALESCE helps:

  • Handle NULL values safely
  • Improve report readability
  • Avoid calculation errors
  • Provide default values

Simple Real-Life Example

Think about:

  • A contact system where some users have no phone number

Without COALESCE

Naresh → NULL
Rahul → 9876543210

With COALESCE

Naresh → Not Available
Rahul → 9876543210

COALESCE Internal Architecture

Read Expressions
      |
      v
Check First Value
      |
      v
NULL?
 /        \
Yes        No
 |          |
Check Next  Return Value
Expression

COALESCE Syntax

COALESCE(expression1,
         expression2,
         expression3,
         ...)

Important Rule

COALESCE returns:

  • The first non-NULL value

Simple COALESCE Example

SELECT COALESCE(NULL,
                NULL,
                'Hello',
                'World');

Result

Hello

Why?

Because:

  • Hello is the first non-NULL value

Employees Table Example

Employee Email Phone
Naresh NULL 9876543210
Rahul rahul@gmail.com NULL
Arjun NULL NULL

COALESCE Query Example

SELECT employee_name,

COALESCE(email,
         phone,
         'No Contact Available') AS contact_info

FROM employees;

Result

Employee Contact Info
Naresh 9876543210
Rahul rahul@gmail.com
Arjun No Contact Available

What Happens Internally?

For Naresh

email → NULL
phone → 9876543210

Result:

9876543210

For Rahul

email → rahul@gmail.com

Result:

rahul@gmail.com

For Arjun

email → NULL
phone → NULL

Result:

No Contact Available

COALESCE Query Flow

Read First Expression
       |
       v
NULL?
 /        \
Yes        No
 |          |
Check Next  Return Result
Expression

Why NULL Handling is Important

NULL values can:

  • Break calculations
  • Create blank reports
  • Cause incorrect outputs

Example Without COALESCE

SELECT salary + bonus
FROM employees;

Problem

If bonus is NULL:

  • Total becomes NULL

Solution Using COALESCE

SELECT salary +
       COALESCE(bonus, 0)

FROM employees;

What Happens?

NULL bonus replaced with:

0

COALESCE with Aggregate Functions

COALESCE is commonly used with:

  • SUM()
  • AVG()
  • COUNT()

Example

SELECT AVG(
    COALESCE(salary, 0)
)

FROM employees;

Purpose

Avoid NULL values affecting calculations.


Difference Between COALESCE and ISNULL

Feature COALESCE ISNULL
Standard SQL Yes No
Multiple Expressions Yes No
Portability High Database-specific

ISNULL Example

ISNULL(phone, 'No Phone')

COALESCE Equivalent

COALESCE(phone, 'No Phone')

Important Difference

COALESCE supports:

  • Multiple fallback values

Example

COALESCE(email,
         phone,
         address,
         'No Data')

COALESCE vs CASE Statement

Feature COALESCE CASE
Purpose NULL handling Conditional logic
Complex Logic Limited Advanced

CASE Equivalent Example

CASE

WHEN email IS NOT NULL
THEN email

WHEN phone IS NOT NULL
THEN phone

ELSE 'No Contact'

END

COALESCE Equivalent

COALESCE(email,
         phone,
         'No Contact')

Why COALESCE is Better Here?

Because:

  • Shorter and cleaner syntax

Performance Consideration

COALESCE usually has:

  • Very low performance overhead

But Excessive Nesting Can:

  • Increase query complexity

Real-Time Banking Example

Banking systems use COALESCE for:

  • Missing customer contact handling
  • Balance calculations
  • Default reporting values

Example

Replace NULL transaction fees with 0.

Real-Time E-Commerce Example

E-commerce platforms use COALESCE for:

  • Product discounts
  • Shipping fee calculations
  • Customer profile handling

Example

COALESCE(discount, 0)

Real-Time Learning Platform Example

Learning platforms use COALESCE for:

  • Student profile data
  • Course analytics
  • Incomplete registration handling

Example

COALESCE(certificate_status,
         'Pending')

COALESCE in Microservices

Microservices use COALESCE for:

  • API response handling
  • Default values
  • Analytics calculations

Advantages of COALESCE

  • Handles NULL values easily
  • Supports multiple fallback values
  • Improves report readability
  • Reduces NULL-related errors

Disadvantages of COALESCE

  • Excessive usage can reduce readability
  • Complex nested expressions may confuse beginners

Best Practices

  • Use COALESCE for NULL-safe calculations
  • Provide meaningful default values
  • Avoid unnecessary nesting
  • Use COALESCE in reporting queries

Common Interview Mistake

Many developers think:

  • COALESCE returns all non-NULL values

Reality

COALESCE returns:

  • Only the first non-NULL value

Related Learning Topics


Professional Interview Answer

The COALESCE function in SQL is used to return the first non-NULL value from a list of expressions. It is commonly used for NULL handling, default value replacement, reporting, calculations, and improving query readability. COALESCE is part of the SQL standard and supports multiple fallback expressions, making it more flexible and portable than database-specific functions like ISNULL. It is widely used in enterprise applications such as banking systems, e-commerce platforms, analytics dashboards, and microservices-based APIs to avoid NULL-related issues and provide meaningful outputs.


Why Interviewers Like This Answer

  • Clearly explains NULL handling
  • Includes internal execution understanding
  • Shows COALESCE vs ISNULL knowledge
  • Provides enterprise-level examples
  • Explains practical use cases

Frequently Asked Questions

What is COALESCE in SQL?

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

Why is COALESCE used?

COALESCE is used to handle NULL values safely.

Can COALESCE accept multiple values?

Yes, COALESCE supports multiple fallback expressions.

What is the difference between COALESCE and ISNULL?

COALESCE is standard SQL and supports multiple expressions, while ISNULL is database-specific.

Does COALESCE improve calculations?

Yes, it prevents NULL values from affecting arithmetic operations.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.