When working with databases, handling NULL values is crucial because NULL represents missing or unknown data. In SQL, NULL does not behave like typical values and requires special handling to ensure accurate query results. This chapter will explore how SQL deals with NULL values, the different functions that help manage them, and best practices for handling NULL effectively.
In SQL, NULL
represents the absence of a value. It is used when the value in a column is unknown, not applicable, or missing. Unlike zero, an empty string, or other placeholder values, NULL
is not a value but a marker that shows a lack of data.
SELECT name, salary
FROM employees;
name | salary |
---|---|
Alice | 50000 |
Bob | NULL |
Charlie | 40000 |
In this case, Bob’s salary is NULL
, meaning it’s unknown or not provided.
The critical concept to understand is that NULL
does not behave like other values in SQL. For example, when performing arithmetic operations or comparisons, NULL
yields unexpected results if not handled properly.
SELECT salary + 1000 AS new_salary
FROM employees;
salary | new_salary |
---|---|
50000 | 51000 |
NULL | NULL |
40000 | 41000 |
The NULL
value in Bob’s salary row does not add up to 1000; instead, it remains NULL
. This demonstrates that operations with NULL
result in NULL
.
To handle NULL
values properly, SQL provides the IS NULL
and IS NOT NULL
operators, which are specifically designed for checking NULL
values.
SELECT name, salary
FROM employees
WHERE salary IS NULL;
name |
---|
Bob |
This query returns rows where the salary
is NULL
, filtering out any rows with non-NULL
values.
SELECT name, salary
FROM employees
WHERE salary IS NOT NULL;
name | salary |
---|---|
Alice | 50000 |
Charlie | 40000 |
Here, only the rows where salary
is not NULL
are selected.
One of the most useful functions for handling NULL
is COALESCE()
. This function returns the first non-NULL
value from a list of values.
COALESCE(expression1, expression2, ..., expressionN)
SELECT name,
COALESCE(salary, 30000) AS salary
FROM employees;
name | salary |
---|---|
Alice | 50000 |
Bob | 30000 |
Charlie | 40000 |
This query replaces any NULL
salary values with 30000
. In this case, Bob’s salary was NULL
, so it is replaced with 30000
.
IFNULL()
FunctionThe IFNULL()
function is similar to COALESCE()
but only takes two arguments. It returns the first argument if it’s not NULL
; otherwise, it returns the second argument.
IFNULL(expression1, expression2)
SELECT name,
IFNULL(salary, 30000) AS salary
FROM employees;
COALESCE()
, this query returns 30000
where salary
is NULL
.NULLIF()
FunctionNULLIF()
returns NULL
if two expressions are equal. If they are not equal, it returns the first expression.
NULLIF(expression1, expression2)
SELECT name,
NULLIF(salary, 40000) AS salary
FROM employees;
name | salary |
---|---|
Alice | 50000 |
Bob | NULL |
Charlie | NULL |
In this query, Charlie’s salary is 40000
, so NULLIF()
returns NULL
for that row. Alice’s salary is returned as-is because it doesn’t match the second argument.
In some SQL dialects (like Oracle), the NVL()
function is used to substitute NULL
with a specified value, similar to IFNULL()
.
NVL(expression1, expression2)
SELECT name,
NVL(salary, 35000) AS salary
FROM employees;
name | salary |
---|---|
Alice | 50000 |
Bob | 35000 |
Charlie | 40000 |
This query replaces Bob’s NULL
salary with 35000
.
When working with aggregate functions (like SUM()
, AVG()
, COUNT()
), NULL
values are often ignored. However, it’s important to be aware of how this can affect the results.
SUM()
:
SELECT SUM(salary) AS total_salary
FROM employees;
total_salary |
---|
90000 |
NULL
values are ignored in the SUM()
function. Only Alice and Charlie’s salaries are summed.
COUNT()
:
SELECT COUNT(salary) AS employee_count
FROM employees;
employee_count |
---|
2 |
The COUNT()
function only counts non-NULL
values, so Bob’s salary is not included in the count.
Always Use IS NULL
and IS NOT NULL
: Direct comparisons with NULL
will not work. Instead, use IS NULL
or IS NOT NULL
to check for NULL
values.
Default Values for NULL: Use functions like COALESCE()
or IFNULL()
to provide default values when working with NULL
values in calculations or data transformations.
Avoid NULL When Possible: Where appropriate, design your database schema to avoid NULL
values by using DEFAULT
values or constraints to ensure data completeness.
Be Cautious with Aggregates: Be mindful that aggregate functions ignore NULL
values unless explicitly handled. Ensure that this behavior is desirable for your use case.
Handling NULL values in SQL is a critical skill for anyone working with databases. NULL represents the absence of a value and behaves differently from other data types. It cannot be compared directly with typical comparison operators and requires the use of specific functions like COALESCE(), IFNULL(), and IS NULL to handle it effectively. Happy coding !❤️