Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: January 27, 2025
When working with SQL queries, it’s common to encounter NULL values in result sets, especially when dealing with data coming from joins and aggregations or involving missing information. If incorrectly handled, NULL values can lead to unexpected behavior in calculations or reports. If the corresponding column is numeric, we can sometimes avoid these errors by replacing NULLs with zeroes.
In this tutorial, we’ll explore how to replace NULL with 0 in SQL results, covering both cross-compatible ANSI-standard approaches and database-specific methods.
We’ll use the Baeldung University Database to demonstrate the queries in this tutorial. All queries have been tested in PostgreSQL 16, MySQL 8, and SQL Server 2022, but they should also work seamlessly across most other versions of these databases.
Replacing NULL with 0 can ensure data consistency in numerical analyses.
NULL values can interfere with calculations, such as in aggregate functions like SUM and AVG, where they are ignored, potentially leading to incorrect results. Moreover, reports and dashboards are easier to read and interpret when numerical columns don’t contain NULL values.
By substituting NULL with 0, we can prevent disruptions in computations and improve the accuracy and clarity of data analysis. However, this isn’t always possible: Sometimes mapping NULLs to zeroes isn’t logically justified.
Let’s introduce a new column lab_hours in the Student table in our database to illustrate the queries covered in this tutorial:
ALTER TABLE Student ADD lab_hours INT;
Now, let’s update some of the rows with values while leaving others as NULL:
UPDATE Student SET lab_hours = 10 WHERE id = 1001;
UPDATE Student SET lab_hours = 15 WHERE id = 1007;
UPDATE Student SET lab_hours = 20 WHERE id = 1011;
UPDATE Student SET lab_hours = 8 WHERE id = 1607;
UPDATE Student SET lab_hours = 12 WHERE id = 1710;
UPDATE Student SET lab_hours = 5 WHERE id = 1621;
UPDATE Student SET lab_hours = 5 WHERE id = 1721;
Now, seven students have non-NULL lab_hours values, and 18 have NULL values. In total, 75 hours were logged.
We can use the AVG() function to calculate the average hours students spent in the lab:
SELECT AVG(lab_hours)
FROM Student;
The query returns the average of 75 / 7 = 10.71 because AVG only considers non-NULL values. However, this result is inaccurate because we want to calculate the average across all students. To achieve this, we must treat NULLs as zeroes, and that way, we’ll get the correct average of 75 / 25 = 3, as there are 25 students in total in the database.
The ANSI standard approaches apply to a wide variety of SQL databases and provide interoperability between different databases and flavors of SQL.
We can use the COALESCE function to replace NULL values with 0. The COALESCE function is a standard SQL function that finds and returns the first non-NULL value from its argument list. Its syntax is:
COALESCE(argument_1, argument_2, ..., argument_n)
In our case, we’ll have only two arguments: the column lab_hours and the constant 0:
SELECT AVG(COALESCE(lab_hours, 0))
FROM Student;
When we execute this query, the COALESCE function ensures that NULL values are replaced with 0 before calculating the average. Consequently, we get the correct result.
Another ANSI-standard method is the CASE WHEN expression.
With it, we explicitly define how NULL values should be handled:
SELECT AVG(
CASE WHEN lab_hours IS NULL THEN 0 ELSE lab_hours END
)
FROM Student;
Here, the CASE expression checks if the lab_hours is NULL and replaces it with zero if that’s the case. Otherwise, it retains the original value of lab_hours.
While slightly more verbose than COALESCE, CASE WHEN offers flexibility for more complex conditions.
In addition to ANSI-standard methods, many databases provide their specific functions to handle NULL values.
MySQL provides the IFNULL function as a concise alternative to COALESCE:
IFNULL(expression, default_value)
The IFNULL function evaluates the expression. If it’s NULL, the function returns the default_value. Otherwise, it returns the value of the expression.
Let’s rewrite the earlier query using the IFNULL function:
SELECT AVG(IFNULL(lab_hours, 0))
FROM Student;
The IFNULL function checks if the lab_hours column is NULL and replaces it with 0 before calculating the average.
Similar to MySQL, SQL Server offers a dedicated function, ISNULL, to replace NULL values with a specified default. It has the same syntax as the IFNULL function of MySQL:
SELECT AVG(ISNULL(lab_hours, 0))
FROM Student;
This replaces the NULL values in the result with 0 and then calculates the average.
Let’s summarize the approaches discussed in this article for replacing NULL with 0:
| Approach | Supported Databases | Syntax |
|---|---|---|
| COALESCE | All Databases | COALESCE(column, default) |
| CASE WHEN | All Databases | CASE WHEN column IS NULL THEN default |
| IFNULL | MySQL | IFNULL(column, default) |
| ISNULL | SQL Server | ISNULL(column, default) |
While these approaches effectively handle NULL values in many scenarios, it’s important to note that replacing NULL with zero isn’t always correct.
For example, when calculating the average GPA of students, treating NULL values as zero can lead to inaccurate results. A NULL value for a student’s GPA indicates that the GPA has not yet been recorded or calculated rather than implying a score logically equivalent to zero. Replacing NULL with zero would distort the calculation.
In this article, we explored the importance of replacing NULL values with zero to ensure cleaner and more interpretable data while avoiding calculation errors. Depending on the database system, this can be achieved using ANSI-standard methods like COALESCE and CASE WHEN, as well as database-specific functions such as IFNULL and ISNULL.