đ The COALESCE function returns the first non-NULL value from a list of expressions. It is commonly used to replace NULL values with default values, combine multiple possible columns, and simplify SQL queries that work with incomplete data.
đ What is COALESCE?
COALESCE evaluates its arguments from left to right and returns the first expression that is not NULL. If every expression is NULL, the function returns NULL.
Information
đ¯ Why Use COALESCE?
COALESCE helps handle missing data gracefully and improves query readability.
- đ Replace NULL values with defaults.
- đ Display meaningful output in reports.
- đ Combine multiple possible columns.
- đ Simplify expressions involving NULL values.
- đ Improve cross-database compatibility.
đ Sample Table
| EmployeeID | EmployeeName | Phone | |
|---|---|---|---|
| 101 | Alice | alice@example.com | 9876543210 |
| 102 | Bob | NULL | 9123456789 |
| 103 | Charlie | NULL | NULL |
đ Basic Syntax
COALESCE Syntax
COALESCE(expression1, expression2, ..., expressionN)The function returns the first expression that is not NULL.
đĄ Example: Replace NULL with a Default Value
Using COALESCE
SELECT
EmployeeName,
COALESCE(Email, 'No Email Available') AS Email
FROM Employees;Employees without an email address will display No Email Available instead of NULL.
đĄ Example: Use Multiple Columns
COALESCE can return the first available contact method.
First Available Contact
SELECT
EmployeeName,
COALESCE(Email, Phone, 'No Contact Information') AS Contact
FROM Employees;The function checks Email first, then Phone, and finally returns the default text if both are NULL.
đĄ Example: Numeric Default Values
Replace NULL Salary Bonus
SELECT
EmployeeName,
Salary + COALESCE(Bonus, 0) AS TotalCompensation
FROM Employees;If an employee has no bonus recorded, 0 is used instead.
đ Example Output
| EmployeeName | Contact |
|---|---|
| Alice | alice@example.com |
| Bob | 9123456789 |
| Charlie | No Contact Information |
âī¸ COALESCE vs Database-Specific Alternatives
| Function | Description | Availability |
|---|---|---|
| COALESCE() | Returns the first non-NULL value. | ANSI SQL (Most Databases) |
| IFNULL() | Replaces NULL with another value. | MySQL, SQLite |
| ISNULL() | Replaces NULL with another value. | SQL Server |
| NVL() | Replaces NULL with another value. | Oracle |
Important
đŧ Real-World Examples
- đ§ Show a default email message when an address is missing.
- đą Display the first available contact method.
- đ° Replace missing bonus values with zero.
- đ Produce cleaner business reports.
- đ Display "Out of Stock" when inventory information is unavailable.
đī¸ Database Compatibility
| Database System | COALESCE Support |
|---|---|
| MySQL | â Fully supported. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Fully supported. |
| Oracle | â Fully supported. |
| SQLite | â Fully supported. |
â ī¸ Common Mistakes
- â Assuming COALESCE() replaces every NULL value in a result set automatically.
- â Mixing incompatible data types in the argument list.
- â Using database-specific NULL functions when cross-database compatibility is important.
- â Forgetting that COALESCE() returns NULL if every argument is NULL.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ COALESCE() returns the first non-NULL expression.
- đ It is commonly used to replace NULL values with defaults.
- đ It accepts two or more expressions.
- đ It is part of the ANSI SQL standard.
- đ It improves query readability and portability.
- đ If every argument is NULL, the result is NULL.