What is the difference between ISNULL() and COALESCE()?
ISNULL() vs COALESCE()
Both functions replace NULL with another value.
Syntax
ISNULL(value, replacement)
COALESCE(value1, value2, ..., defaultValue)
Main differences
| Feature | ISNULL() |
COALESCE() |
|---|---|---|
| Standard | SQL Server-specific | ANSI SQL standard |
| Number of arguments | Exactly two | Two or more |
| Result | Returns replacement if first value is NULL |
Returns the first non-NULL value |
| Return data type | Uses the first argument’s data type | Uses the data type with the highest precedence |
| Nullability metadata | Often treated as not nullable | May be treated as nullable |
| Evaluation | Function | Translated internally to a CASE expression |
ISNULL() example
SELECT ISNULL(Bonus, 0) AS Bonus
FROM Employees;
If Bonus is NULL, it returns 0.
COALESCE() example
SELECT COALESCE(MobileNumber, HomeNumber, OfficeNumber, 'Not Available')
FROM Employees;
It returns the first non-NULL value.
Important data-type difference
ISNULL() normally uses the data type and length of its first argument.
DECLARE @Name VARCHAR(5) = NULL;
SELECT ISNULL(@Name, 'Mohamed');
Result:
Moham
The result is truncated because @Name is VARCHAR(5).
COALESCE() determines the result type based on SQL Server’s data-type precedence and expression rules:
DECLARE @Name VARCHAR(5) = NULL;
SELECT COALESCE(@Name, 'Mohamed');
Result:
Mohamed
In this example, the longer literal can determine the returned string length.
When to use
Use ISNULL() when:
-
You are working only with SQL Server.
-
You need to replace one value with one default value.
-
You want the first argument to control the result type.
ISNULL(Salary, 0)
Use COALESCE() when:
-
You need the first available value from multiple columns.
-
You want portable, standard SQL.
-
You understand its data-type precedence behaviour.
COALESCE(Email, MobileNumber, 'Not Available')
Interview answer
ISNULL()is SQL Server-specific and accepts exactly two arguments.COALESCE()is ANSI-standard SQL and accepts multiple arguments, returning the first non-NULL value. They can also differ in result data type and nullability.