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.
NULL
in SQL is a placeholder for missing or unknown data. It’s essential to remember that NULL
is not the same as:
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
.
Suppose we have a table employees
as shown below:
ID | Name | Department | Salary | Manager ID |
---|---|---|---|---|
1 | John Doe | HR | $5000 | 10 |
2 | Jane Smith | IT | $6000 | 11 |
3 | David Green | Sales | $5500 | N/A |
4 | Sarah Brown | IT | NULL | 11 |
David Green
has no assigned manager_id
, and Sarah Brown
has no salary
information.
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
.
SELECT column1, column2
FROM table_name
WHERE column_name IS NULL;
SELECT column1, column2
FROM table_name
WHERE column_name IS NOT NULL;
Let’s find all employees whose manager_id
is NULL
.
SELECT name, department
FROM employees
WHERE manager_id IS NULL;
Name | Department |
---|---|
David Green | Sales |
David Green
is the only employee with no assigned manager (manager_id
is NULL
).
Now, let’s find all employees who have a salary
.
SELECT name, salary
FROM employees
WHERE salary IS NOT NULL;
Name | Salary |
---|---|
John Doe | $5000 |
Jane Smith | $6000 |
David Green | $5500 |
Sarah Brown
was excluded from the result because her salary
is NULL
.
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.
SELECT * FROM employees WHERE salary = NULL;
This query will not return any rows, even though some employees have NULL
values for salary
.
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
.
COALESCE(expression1, expression2, ..., expression_n);
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
.
SELECT name, COALESCE(salary, 4000) AS salary
FROM employees;
Name | Salary |
---|---|
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
.
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
.
Let’s calculate the total and average salary of all employees.
SELECT SUM(salary) AS total_salary, AVG(salary) AS average_salary
FROM employees;
Total Salary | Average Salary |
---|---|
$16500 | $5500 |
The SUM
and AVG
functions ignored NULL
salaries and only considered non-NULL
values.
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.
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.
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
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.
Let’s find the distinct salaries of employees, including NULL
.
SELECT DISTINCT salary FROM employees;
Salary |
---|
$5000 |
$6000 |
$5500 |
NULL |
Only one NULL
appears in the result, even though there might be multiple rows with NULL
salaries.
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.
CASE
WHEN condition THEN result
ELSE result
END;
We want to display “No Manager” for employees without a manager_id
.
SELECT name,
CASE
WHEN manager_id IS NULL THEN 'No Manager'
ELSE manager_id
END AS manager
FROM employees;
Name | Manager |
---|---|
John Doe | 10 |
Jane Smith | 11 |
David Green | No Manager |
Sarah Brown | 11 |
Some string functions treat NULL
values differently. For instance, concatenating a string with NULL
results in NULL
.
Let’s try concatenating employee names with a NULL
middle name.
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 !❤️