Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres NOT IN (null) gives no result

Tags:

I'm using Postgres with this query

select  * from Entity this_  where  (this_.ID not in (null)) 

Why does this give me no results? I would expect to get all rows where id is not null

with

(this_.ID not in (1)) 

i get the expected results

like image 904
wutzebaer Avatar asked Jul 26 '13 08:07

wutzebaer


People also ask

How do I deal with not null in PostgreSQL?

Here is an example of how to use the PostgreSQL IS NOT NULL condition in a SELECT statement: SELECT * FROM employees WHERE first_name IS NOT NULL; This PostgreSQL IS NOT NULL example will return all records from the employees table where the first_name does not contain a null value.

How do I return NULL values in PostgreSQL?

PostgreSQL NULLIF function syntaxThe NULLIF function returns a null value if argument_1 equals to argument_2 , otherwise it returns argument_1 .

WHAT DOES NOT NULL mean in PostgreSQL?

The not-null constraint in PostgreSQL ensures that a column can not contain any null value. This is a column constraint. No name can be defined to create a not-null constraint. This constraint is placed immediately after the data-type of a column. Any attempt to put NULL values in that column will be rejected.

Is NULL in PostgreSQL?

PostgreSQL IS NOT NULL operator.


2 Answers

The result of [not] in (null) will always be null. To compare to null you need is [not] null or is [not] distinct from null

select * from Entity this_  where this_.ID is not null 

If you want where (ID not in (1,null)) as in your comment you can do

where ID is not null and ID not in (1) 
like image 113
Clodoaldo Neto Avatar answered Sep 20 '22 12:09

Clodoaldo Neto


PostgreSQL uses NULL as undefined value.
What you're asking is to return the items that are not in a list or an undefined value. Since undefined means that you do not know what's inside, PostgreSQL does not return any item because simply can not respond to the request.
While the request:

select * from Entity where id in (1, null) 

can return the records, because if it finds an element with ID = 1 knows that is in the collection
the request:

select * from Entity where (ID not in (1, null)) 

can not be satisfied because the null value can be any value.

like image 37
AndreaBoc Avatar answered Sep 21 '22 12:09

AndreaBoc