0

Consider:

SELECT * FROM Entity e WHERE e.Status <> ANY(ARRAY[1,2,3]);

Here Status is a nullable integer column. Using the above query, I am unable to fetch the records whose status value is NULL.

SELECT * FROM Entity e WHERE (e.Status is NULL OR e.Status = 4);

This query does the trick. Why was the first query not working as expected?

2 Answers 2

1

NULL kinda means "unknown", so the expressions

NULL = NULL

and

NULL != NULL

are neither true nor false, they're NULL. Because it is not known whether an "unknown" value is equal or unequal to another "unknown" value.

Since <> ANY uses an equality test, if the value searched in the array is NULL, then the result will be NULL.

So your second query is correct.

Sign up to request clarification or add additional context in comments.

Comments

0

It is spelled out in the docs Array ANY:

If the array expression yields a null array, the result of ANY will be null. If the left-hand expression yields null, the result of ANY is ordinarily null (though a non-strict comparison operator could possibly yield a different result). Also, if the right-hand array contains any null elements and no true comparison result is obtained, the result of ANY will be null, not false (again, assuming a strict comparison operator). This is in accordance with SQL's normal rules for Boolean combinations of null values.

FYI:

e.Status is NULL OR e.Status = 4

can be shortened to:

e_status IS NOT DISTINCT FROM 4

per Comparison operators.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.