Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does 'in' clause works in oracle

select 'true' from dual where 1 not in (null,1);

when we execute this which will result nothing

what my question is:

is the above query is logically equivalent to

select 'true' from dual where 1 != null and 1 != 1;

which will result nothing just as above statement

Please clarify?

like image 519
Maddy Avatar asked Jul 03 '12 12:07

Maddy


People also ask

What does in clause do in Oracle?

Description. The Oracle IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

How does in clause work in SQL?

The SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

Can we use in clause in case Oracle?

Introduction to Oracle CASE expression You can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .

What is the limit of in clause in Oracle?

In Oracle we can only put up to 1000 values into an IN clause.


4 Answers

Correct (but note that IN is an operator, not a clause and it works like this in SQL in general, not only for Oracle).

where 1 not in (null,1)

is equivalent to:

where 1 != null and 1 != 1

which should really be written as:

WHERE 1 NOT IN (NULL, 1)

and

WHERE 1 <> NULL AND 1 <> 1

which is the same as:

WHERE (1 <> NULL) AND (1 <> 1)

which evaluates to:

WHERE UNKNOWN AND FALSE

and further as:

WHERE FALSE

So, it correctly returns no rows.


Notice that if you had WHERE 1 NOT IN (NULL, 2), it would evaluate to WHERE UNKNOWN (left as an exercise) and no rows would be returned either.

like image 72
ypercubeᵀᴹ Avatar answered Sep 26 '22 22:09

ypercubeᵀᴹ


The issue of your script in comparing with NULL value. You should use

column is null and column = 1

Actually NULL is an undefined value. Any comparation with NULL gives neither True nor False but NULL. Even NULL = NULL

That's why your 1 not in (null,1) doesn't work.

like image 43
Maksym Polshcha Avatar answered Sep 27 '22 22:09

Maksym Polshcha


Yes they are.

select something from table where column not in (1,2,3);

is equivalent to

select something from table where column != 1 and column != 2 and column != 3;
like image 29
manurajhada Avatar answered Sep 27 '22 22:09

manurajhada


The IN statement is a collection of OR statements, while NOT IN is a collection of AND statements - but it is also not equal to.

So the NOT IN is equivalent to:

1 <> NULL
AND 1 <> 1
AND ...

While the IN would be equivalent to:

1 = NULL
OR 1 = 1
OR ...

Note that having NULL in the collection will not work, due to the quirky nature of NULL.

like image 37
John D Avatar answered Sep 24 '22 22:09

John D