Menu

SQL Null Functions – Understanding SQL Null Functions

Written by Jagdeesh | 3 min read

Understanding SQL NULL functions, explaining their syntax and illustrating their use with practical examples.

SQL is a standard language for managing data held in a relational database management system. Among the vast number of functions and capabilities provided by SQL, NULL functions are a crucial feature that every SQL user should understand.

What is NULL in SQL?

In SQL, NULL signifies that a particular field has no value or is essentially missing. It’s important to understand that NULL doesn’t mean zero or blank, it means no entry was made or an explicit NULL was given. This can happen when the value is unknown or not applicable.

SQL Null Functions

SQL NULL functions are used to handle NULL values in a database. The most common NULL functions are ISNULL(), COALESCE(), and NULLIF().

1. ISNULL():

The ISNULL() function is used to replace NULL values with a specified value.

The syntax of the ISNULL() function is as follows:

output
ISNULL(expression, value_if_null)
  • expression: the field or value to check for NULL.

  • value_if_null: the value to return if the expression is NULL.

For example, let’s consider a table, Employees, where some entries in the Salary column might be NULL:

output
| ID | Name   | Salary |
|----|--------|--------|
| 1  | John   | 5000   |
| 2  | James  | NULL   |
| 3  | Julia  | 6000   |

The following SQL statement replaces NULL values in the “Salary” column with “0”:

sql
SELECT Name, ISNULL(Salary, 0) AS Salary
FROM Employees;

The result is:

output
| Name  | Salary |
|-------|--------|
| John  | 5000   |
| James | 0      |
| Julia | 6000   |

2. COALESCE():

COALESCE() function is used to return the first non-null value in a list. If all arguments are NULL, COALESCE returns NULL.

Here is the basic syntax of COALESCE():

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

For example, using the same Employees table, if you have another column “Bonus” which might also contain NULL values:

output
| ID | Name   | Salary | Bonus |
|----|--------|--------|-------|
| 1  | John   | 5000   | NULL  |
| 2  | James  | NULL   | 1000  |
| 3  | Julia  | 6000   | NULL  |

The following SQL statement returns the first non-null value from the “Salary” or “Bonus” column:

sql
SELECT Name, COALESCE(Salary, Bonus) AS Earnings
FROM Employees;

The result is:

output
| Name  | Earnings |
|-------|----------|
| John  | 5000     |
| James | 1000     |
| Julia | 6000     |

3. NULLIF():

NULLIF() function returns NULL if two expressions are equal. If the expressions are not equal, the first expression is returned.

The syntax of NULLIF() is as follows:

output
NULLIF(expression1, expression2)

Suppose you have a table Products with “Price” and “Discounted_Price” columns:

output
| ID | Product | Price | Discounted_Price |
|----|---------|-------|------------------|
| 1  | A       | 20    | 15               |
| 2  | B       | 30    | 30               |
| 3  | C       | 40    | 35               |

The following SQL statement returns NULL if the “Price” and “Discounted_Price” are the same, otherwise, it returns “Price”:

sql
SELECT Product, NULLIF(Price, Discounted_Price) AS Effective_Price
FROM Products;

The result is:

output
| Product | Effective_Price |
|---------|-----------------|
| A       | 20              |
| B       | NULL            |
| C       | 40              |

This means product B is not on discount as the original price and the discounted price are the same.

Conclusion

SQL NULL functions offer powerful ways to handle NULL values and provide meaningful data interpretation, which is crucial for sound data analysis.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science