🎁 Exclusive offer: Get EXTRA Bits and Celebrate Bybit's 6th Birthday With $2.2M Prize Pool. Act Now!

Code has been added to clipboard!

Using the SQL ISNULL Function

Reading time 2 min
Published Aug 9, 2017
Updated Oct 10, 2019

SQL NULL Functions: Main Tips

  • The function ISNULL() is needed in order to define the way we use NULL.
  • There are other three functions which will produce a similar result ( COALESCE(), IFNULL(), NVL() ).

Syntax of SQL NULL

Below you can see the table "Products":

ID Name Supplier_ID Category_ID Unit Price Unit_In_Stock Unit_On_Order
1 Cucumber 3 3 5 crates 19 17 16
2 Tomato 3 3 6 crates 20 24 Null
3 Cheese 3 4 5 kg boxes 11 11 6
4 Milk 4 4 5 L bottles 22 56 12
5 Bread 4 4 36 boxes 26 26 1

Have in mind that UnitOnOrder has NULL values.

In the code example below we can see that the result will be NULL if the columns "Unit_On_Order" will contain NULL values:

Example
SELECT Name, Price * (Unit_In_Stock + Unit_On_Order)
FROM Products

The examples below show that if the column "UnitOnOrder" is a value NULL the enumeration will proceed as usual. The function ISNULL() brings back a zero when NULL is the value:

MS Access

Example
SELECT Name, Price * (Unit_In_Stock + IIF(ISNULL(Unit_On_Order), 0, Unit_On_Order))
FROM Products;
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

SQL Server

Example
SELECT Name, Price * (Unit_In_Stock + ISNULL(Unit_On_Order, 0))
FROM Products

Oracle

Here we start using other functions rather than ISNULL() because of the database-system compatibility. In oracle we use the function NVL():

Example
SELECT Name, Price * (Unit_In_Stock + NVL(Unit_On_Order, 0))
FROM Products

MySQL

Just like in oracle, MySQL requires a different function to work. There are two that work:

First, we have the function IFNULL():

Example
SELECT Name, Price * (Unit_In_Stock + IFNULL(Unit_On_Order, 0))
FROM Products

The second function is COALESCE():

Example
SELECT Name, Price * (Unit_In_Stock + COALESCE(Unit_On_Order, 0))
FROM Products