Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Snowflake exclude matching NULL values in WHERE clause?

Suppose I have a table called people where one of the values in the Name column is null:

ID Name
1 John
2 Mary
3 null

I want to select all rows where Name is not equal to "John", so I do:

SELECT * FROM people WHERE Name <> 'John'

In Snowflake, this returns only "Mary" and excludes the last row with a null value for Name. If I want to include null values (expected behavior), I have to do:

SELECT * FROM people WHERE (Name <> 'John' OR Name is NULL)

Is this expected behavior? And does anyone know how to configure Snowflake to return NULL rows as well?

Since by definition, the absence of a value (i.e. NULL) should match the condition Name <> 'John'

like image 276
Ralph Avatar asked Mar 16 '26 18:03

Ralph


2 Answers

All databases handle NULLs the same way (well, pretty much so). Almost any comparison involving NULL -- with the notable exceptions of IS NULL and IS NOT NULL and a few others -- returns NULL. WHERE clauses and CASE expressions treat NULL values the same as "false".

Fortunately, Snowflake also implements the standard SQL NULL-safe operator, IS DISTINCT FROM. So you can write the logic as:

SELECT p.*
FROM people p
WHERE Name IS DISTINCT FROM 'John'
like image 72
Gordon Linoff Avatar answered Mar 19 '26 08:03

Gordon Linoff


It is common database expected behaviour when every comparison with null returns false.

In other words:

if some_variable = null -- always false even if some_variable was assigned null
if some_variable <> null -- always false

It is because "null" means "nothing" and it is hard to compare nothing to nothing.

As I can see from the docs you can avoid writing "or name is null" by using NVL function which takes 2 parameters and returns second parameter if first one is null.

For example:

nvl(1, 3) = 1
nvl(null, 3) = 3

So your query might be looking like this:

SELECT * FROM people WHERE nvl(Name,'John') <> 'John';
like image 43
ekochergin Avatar answered Mar 19 '26 07:03

ekochergin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!