Handling NULL Values in SQL

In SQL, NULL represents the absence of a value or unknown data in a database. It's different from zero, an empty string, or any other default value because it signifies "no value at all." Properly handling NULL values is essential to writing accurate and meaningful SQL queries, as these values can affect data retrieval, comparison, and even database integrity.

This chapter will cover what NULL is, how to handle it in SQL queries, and the functions and operators used to work with NULL. By the end of this chapter, you’ll be able to manage NULL values effectively in various contexts like data retrieval, aggregation, and conditions.

Understanding NULL in SQL

NULL in SQL is a placeholder for missing or unknown data. It’s essential to remember that NULL is not the same as:

  • 0 (zero) for numeric fields,
  • An empty string for text fields,
  • False for Boolean fields.

NULL has a unique behavior when compared to other values. In SQL, any operation involving NULL results in NULL. For instance, if you add, multiply, or compare NULL to another value, the result is NULL.

Example 1: NULL in a Table

Suppose we have a table employees as shown below:

IDNameDepartmentSalaryManager ID
1John DoeHR$500010
2Jane SmithIT$600011
3David GreenSales$5500N/A
4Sarah BrownITNULL11

David Green has no assigned manager_id, and Sarah Brown has no salary information.

Using IS NULL and IS NOT NULL

Since NULL cannot be compared directly with values (e.g., = NULL or != NULL won’t work), SQL provides special operators to check for NULL values: IS NULL and IS NOT NULL.

Syntax:

				
					SELECT column1, column2
FROM table_name
WHERE column_name IS NULL;

				
			
				
					SELECT column1, column2
FROM table_name
WHERE column_name IS NOT NULL;

				
			

Example 2: Using IS NULL

Let’s find all employees whose manager_id is NULL.

Query:

				
					SELECT name, department
FROM employees
WHERE manager_id IS NULL;

				
			

Output:

NameDepartment
David GreenSales

David Green is the only employee with no assigned manager (manager_id is NULL).

Example 3: Using IS NOT NULL

Now, let’s find all employees who have a salary.

Query:

				
					SELECT name, salary
FROM employees
WHERE salary IS NOT NULL;

				
			

Output:

NameSalary
John Doe$5000
Jane Smith$6000
David Green$5500

Sarah Brown was excluded from the result because her salary is NULL.

NULL in Comparisons

One of the trickiest aspects of working with NULL values is understanding that NULL cannot be compared to any other value, even another NULL. In SQL, comparing NULL with anything (even NULL) results in NULL, which means neither true nor false.

For instance, the following query will return an empty result set:

				
					SELECT * FROM employees WHERE salary = NULL;

				
			

Instead, you must use IS NULL to perform this kind of check.

Example 4: Common Pitfall

Incorrect Query:

				
					SELECT * FROM employees WHERE salary = NULL;

				
			

This query will not return any rows, even though some employees have NULL values for salary.

Using COALESCE to Handle NULLs

The COALESCE() function is used to handle NULL values by returning the first non-NULL value from a list of expressions. If all expressions are NULL, the function returns NULL.

Syntax:

				
					COALESCE(expression1, expression2, ..., expression_n);

				
			

Example 5: Using COALESCE to Replace NULLs

Let’s assume we want to display the salary of employees, but for those who don’t have a salary (i.e., NULL), we want to display a default value of 4000.

Query:

				
					SELECT name, COALESCE(salary, 4000) AS salary
FROM employees;

				
			

Output:

NameSalary
John Doe$5000
Jane Smith$6000
David Green$5500
Sarah Brown$4000

Sarah Brown had a NULL salary, which has now been replaced by the default value of 4000 using COALESCE.

NULL in Aggregation

Aggregating data (using functions like SUM, AVG, COUNT, etc.) in the presence of NULL values can lead to unexpected results. Most aggregate functions in SQL ignore NULL values except for COUNT.

Example 6: SUM and AVG with NULLs

Let’s calculate the total and average salary of all employees.

Query:

				
					SELECT SUM(salary) AS total_salary, AVG(salary) AS average_salary
FROM employees;

				
			

Output:

Total SalaryAverage Salary
$16500$5500

The SUM and AVG functions ignored NULL salaries and only considered non-NULL values.

Handling NULL in JOINs

When performing JOIN operations, NULL values can affect how rows are matched between tables. Understanding the behavior of NULL in different types of JOINs is critical.

Example 7: NULL in LEFT JOIN

Let’s assume we have two tables: employees and departments. We want to retrieve all employees and their corresponding departments, even if some employees don’t belong to any department.

Query:

				
					SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;

				
			

If an employee has no department_id, the result will display NULL for the department.

NULL with DISTINCT and UNION

NULL values are treated as equal in the context of DISTINCT and UNION operations. This means that only one NULL will appear in the result, even if multiple rows have NULL in the same column.

Example 8: Using DISTINCT with NULLs

Let’s find the distinct salaries of employees, including NULL.

Query:

				
					SELECT DISTINCT salary FROM employees;

				
			

Output:

Salary
$5000
$6000
$5500
NULL

Only one NULL appears in the result, even though there might be multiple rows with NULL salaries.

NULL in Conditional Statements (CASE and IFNULL)

The CASE statement allows you to handle NULL values conditionally in queries. Similarly, the IFNULL() function (or ISNULL() in some SQL dialects) provides a shorthand way to replace NULL values with a specified value.

Syntax of CASE:

				
					CASE
    WHEN condition THEN result
    ELSE result
END;

				
			

Example 9: Using CASE to Handle NULL

We want to display “No Manager” for employees without a manager_id.

Query:

				
					SELECT name, 
    CASE 
        WHEN manager_id IS NULL THEN 'No Manager'
        ELSE manager_id
    END AS manager
FROM employees;

				
			

Output:

NameManager
John Doe10
Jane Smith11
David GreenNo Manager
Sarah Brown11

NULL in String Functions

Some string functions treat NULL values differently. For instance, concatenating a string with NULL results in NULL.

Example 10: CONCAT with NULL

Let’s try concatenating employee names with a NULL middle name.

Query:

				
					SELECT CONCAT(name, ' ', middle_name) AS full_name
FROM employees;

				
			

If middle_name is NULL, the result will be NULL. You can use COALESCE to handle this:

				
					SELECT CONCAT(name, ' ', COALESCE(middle_name, '')) AS full_name
FROM employees;

				
			

Handling NULL values in SQL is crucial to ensuring accurate results in your queries. Understanding how NULL behaves in comparisons, joins, aggregations, and functions is essential for writing robust SQL code. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India