## Sunday, October 3, 2021

### How to handle NULL values in Snowflake

Are you facing challenges while handling NULL values in snowflake? Are you also interested in knowing what are the things we need to consider while columns in the query contain NULL values? In this article, we will learn details about handling NULL values.

A) What are NULL values?

Many times NULL value is referred to as no value. some systems do not have NULL concepts. In technical terms, the NULL value is a reference to an empty area of the memory. some systems handle differently than others. Snowflake has its own way of handling NULL values.

B) What are the rules for handling NULL values in Snowflake?

Here is the list of rules  -

1. Not null to null value comparison :

we compare not null value with the null value it returns NULL

e.g  'ABC ' =NULL returns NULL

2. Equality or inequality comparison :

If we use inequality comparisons such as less than or greater than it results in a NULL                              value.

e.g 'ABC' > NULL returns NULL

3. Comparision of NULL values :

If we compare one NULL value with another NULL value then it results in a NULL value.

e,g . NULL= NULL results NULL

4. Best way of comparing NULL values is

IS NULL or IS NOT NULL

5. Function to null safe equality

We can use the EQUAL_NULL function to check NULL safe equality.

e.g. equal_null ('ABC' , null) will return false.

6. Aggregate function dismisses NULL values.

Assume that one of the fields in the snowflake contains the below values -

1,2, NULL , 3, NULL 6, 1

If we use aggregate function 4 AVG on this field then it will return the result as                                              3 i.e(1+2+3+6)/4

i.e. It ignores NULL values.

7. Real average value

In case we need a real average in the above example we need to use the SUM function, where the NULL value is 0

i.e. SUM(<value>)/count (*)

e.g. sum(1+2+0+3+0+6)/6

i.e. 12/6 = 2

8. The empty string in snowflake is NOT NULL

e.g. "IS NULL will returns false.

9. Count (*) returns the total count of rows in the table

10. Count (<column_name>) returns the count of rows with not null values only 