Data Engineering Spark SQL - Spark SQL Functions - Dealing with Nulls

Let us understand how to handle nulls using specific functions in Spark SQL. Let us start the spark context for this Notebook so that we can execute the code provided.

Key Concepts Explanation

Null Handling Functions

By default, if we try to add or concatenate null to another column or expression or literal, it will return null.

If we want to replace null with some default value, we can use nvl. For not null values, nvl returns the original expression value. We can also utilize coalesce in place of nvl.

Multiple Value Handling

coalesce returns the first non-null value if we pass multiple arguments to it. nvl2 can be used to perform one action when the value is not null and another action when the value is null.

Conditional Logic Using CASE

CASE WHEN ELSE END can be used for any conditional logic handling.

Hands-On Tasks

  1. Replace the commission_pct with 0 if it is null in the sales data.
  2. Calculate the commission amount and handle null commission_pct values by assuming them to be 0.


In this article, we discussed how to handle NULL values when working with Spark SQL. By using functions like nvl, coalesce, and conditional logic with CASE WHEN ELSE END, we can easily manage NULLs in our data processing operations. It is essential to handle NULL values properly to prevent errors and ensure accurate analysis.

Watch the video tutorial here