Handling NULL values with SQL

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.

NULL in SQL

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.

Example:

				
					SELECT name, salary
FROM employees;

				
			
namesalary
Alice50000
BobNULL
Charlie40000

In this case, Bob’s salary is NULL, meaning it’s unknown or not provided.

Behavior of NULL in SQL

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.

Example of Addition:

				
					SELECT salary + 1000 AS new_salary
FROM employees;

				
			
salarynew_salary
5000051000
NULLNULL
4000041000

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.

Using IS NULL and IS NOT NULL

To handle NULL values properly, SQL provides the IS NULL and IS NOT NULL operators, which are specifically designed for checking NULL values.

Example: Checking for NULLs

				
					SELECT name, salary
FROM employees
WHERE salary IS NULL;

				
			

Output:

name
Bob

This query returns rows where the salary is NULL, filtering out any rows with non-NULL values.

Example: Excluding NULLs

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

				
			

Output:

namesalary
Alice50000
Charlie40000

Here, only the rows where salary is not NULL are selected.

Handling NULL with the COALESCE() Function

One of the most useful functions for handling NULL is COALESCE(). This function returns the first non-NULL value from a list of values.

Syntax:

				
					COALESCE(expression1, expression2, ..., expressionN)

				
			

Example:

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

				
			

Output:

namesalary
Alice50000
Bob30000
Charlie40000

This query replaces any NULL salary values with 30000. In this case, Bob’s salary was NULL, so it is replaced with 30000.

Handling NULL with IFNULL() and NULLIF() Functions

IFNULL() Function

The 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.

Syntax:

				
					IFNULL(expression1, expression2)

				
			

Example:

				
					SELECT name, 
       IFNULL(salary, 30000) AS salary
FROM employees;

				
			
  • Similar to COALESCE(), this query returns 30000 where salary is NULL.

NULLIF() Function

NULLIF() returns NULL if two expressions are equal. If they are not equal, it returns the first expression.

Syntax:

				
					NULLIF(expression1, expression2)

				
			

Example:

				
					SELECT name, 
       NULLIF(salary, 40000) AS salary
FROM employees;

				
			

Output:

namesalary
Alice50000
BobNULL
CharlieNULL

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.

The NVL() Function

In some SQL dialects (like Oracle), the NVL() function is used to substitute NULL with a specified value, similar to IFNULL().

Syntax:

				
					NVL(expression1, expression2)

				
			

Example:

				
					SELECT name, 
       NVL(salary, 35000) AS salary
FROM employees;

				
			

Output:

namesalary
Alice50000
Bob35000
Charlie40000

This query replaces Bob’s NULL salary with 35000.

Handling NULL in Aggregate Functions

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.

Example with SUM():

				
					SELECT SUM(salary) AS total_salary
FROM employees;

				
			

Output:

total_salary
90000

NULL values are ignored in the SUM() function. Only Alice and Charlie’s salaries are summed.

Example with COUNT():

				
					SELECT COUNT(salary) AS employee_count
FROM employees;

				
			

Output:

employee_count
2

The COUNT() function only counts non-NULL values, so Bob’s salary is not included in the count.

Best Practices for Handling NULLs

  • 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 !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India