SQL COALESCE Function

Quick summary: The SQL COALESCE() function returns the first non-NULL value from a list.

SQL COALESCE Syntax

COALESCE(value1, value2, ..., valueN)
SQL

SQL COALESCE Basic examples

SELECT COALESCE(NULL, 'default');
SQL
Output:
default

Returns the first non-NULL value.

SQL COALESCE Real-world usage

SELECT COALESCE(phone, email) AS contact FROM users;
SQL
Output:
Primary contact value

Provides fallbacks for missing data.

SQL COALESCE Edge cases

SELECT COALESCE(NULL, NULL);
SQL
Output:
NULL

Returns NULL if all arguments are NULL.

SQL COALESCE Common mistakes

Using COALESCE instead of defaults

Overuse can hide schema issues.

Incorrect
COALESCE(price, 0)
Correct
ALTER TABLE products SET DEFAULT 0

Prefer schema defaults when possible.

SQL COALESCE Frequently Asked Questions

What does COALESCE() do in SQL?

Returns the first non-NULL value.

Use case of COALESCE()?

Handling NULL values.

Syntax?

COALESCE(value1, value2, ...).

Common mistake?

Wrong data types.

Return type?

Type of first non-NULL value.

Performance?

Fast.

Alternative?

IFNULL (MySQL).

Handles multiple values?

Yes.

Used in SELECT?

Yes.

Used in WHERE?

Yes.

Used in reports?

Yes.

Best practice?

Use for defaults.

SQL COALESCE Related SQL Keywords