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;
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
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 03/03/2021
3176 People Used
Only 97 Left
Rating
5.0