How does 'in' clause works in oracle - sql

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?

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.

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.

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;

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.

Yes. It is correct. Also NULL values should be compared with IS NULL

Related

Conditional Where Statement

I am trying to setup a conditional statement that will determine which WHERE clause to use.
I need to check 2 separate conditions, one on each side of the AND and if results are greater than 1 then use a particular statement if not then use nothing
Like this logic but in pdw sql
WHERE
if cte1.counte > 1 then 'cte1.built is not null' else ''
AND
if cte2.countd > 1 then 'cte2.demo is not null' else ''
possible combinations:
WHERE CTE1.BUILD IS NOT NULL
WHERE CTE1.BUILD IS NOT NULL AND CTE2.DEMO IS NOT NULL
WHERE CTE2.DEMO IS NOT NULL
BLANK
Is this possible to do?
Thanks in advance
Something like this:
WHERE (cte1.counte > 1 and cte1.built is not null or cte1.counte <= 1) and
(cte2.countd > 1 and cte2.demo is not null or cte2.countd <= 1)

Is null > 0 (integer) [duplicate]

This question already has answers here:
What is NULL in SQL?
(5 answers)
Closed 8 years ago.
If I have in my sql something like that:
SELECT *
FROM SOMETABLE
WHERE ((SELECT ONECOLUMN FROM ANOTHERTABLE WHERE ID = 42) > 0)
If I got a NULL in ONECOLUMN it will be greater then 0?
As NULL means "not known", NULL is not greater than 0. It's not smaller either. It's not known. Hence NULL > 0 results in NULL, rather than in TRUE or FALSE.
NULL is not greater than zero.
NULL is not equal to zero.
NULL is not less than zero.
Any given integer you might choose would meet exactly one of these three conditions. But not NULL.
NULL isn't an integer. It's a marker indicating that a value is not present. This could mean that a value exists, but is unknown. It could also indicate that no value exists in this context.
You can use the ISNULL function to find out whether a NULL is present instead of a value. But if you compare a value with zero, and there is a NULL in place of the value, you won't get either TRUE or FALSE as a result.
If you are confused, you're in good company.
How about checking for NULL and then returning 0 if true otherwise return the value of ONECOLUMN:
SELECT *
FROM SOMETABLE
WHERE (
SELECT CASE WHEN ISNULL(ONECOLUMN) THEN 0 ELSE ONECOLUMN END
FROM ANOTHERTABLE
WHERE ID = 42
) > 0
Null is unknown and it cannot be compared to any value. It cannot be greater or lesser.
SELECT *
FROM SOMETABLE
WHERE (
IsNull((SELECT ONECOLUMN
FROM ANOTHERTABLE
WHERE ID = 42), IntValue) > 0
)
If you need to accept the Null so IntValue can be 1 otherwise -1 is OK.

Comparing 2 null datetimes not returning equal

In an update trigger I am comparing 2 nullable datetimes, that both happen to be null in my current test and it is returning that they are not equal.
#ExpirationDateChanged = case when i.ExpirationDate = d.ExpirationDate then 0 else 1 end
In this case i.ExpirationDate and d.Expiration date are null but instead of getting the expected 0, I am getting 1.
Can anyone explain this behavior?
NULL is not equal to NULL using equality comparison.
Comparing NULL values for equality is UNKNOWN under ANSI setting (the default)
You need to add an extra condition to explicitly test both columns for NULL using IS NULL:
#ExpirationDateChanged = case
when (i.ExpirationDate IS NULL AND d.ExpirationDate IS NULL) OR
i.ExpirationDate = d.ExpirationDate then 0
else 1
end

NULL IN (1,2,NULL) returns false

Why does this SQL-statement return 0?
SELECT CASE WHEN NULL IN (9,1,NULL) THEN 1 ELSE 0 END
SQL is based on three-valued logic, where there are three truth values: TRUE, FALSE, and UNKNOWN. The special NULL value is a placeholder for "missing data" and if you compare something, anything, with missing data the result is unknown.
For example, is <missing data> equal to <missing data>? It's impossible to know, so the result is UNKNOWN.
In this particular case, you are trying to find out if <missing data> is in a given list: since the data is missing, it's impossible to know if it's in the list, and the query returns 0.
SELECT CASE WHEN NULL IN (9,1,NULL) THEN 1 ELSE 0 END
I don't know what RDBMS you are using since some of them has configuration on how NULL will be treated.
NULL IN (9, 1, NULL)
can be written as
(NULL = 9) OR (NULL = 1) OR (NULL = NULL)
and not of them were TRUE nor FALSE. They are all NULL. Since there are only two paths in the CASE statement, it falls under ELSE block.
It depends on how NULL is treated in the specific server.
For instance in SQL SERVER you may set ANSI_NULLS off and have it return 1:
SET ANSI_NULLS OFF
SELECT CASE WHEN NULL IN (9,1,NULL) THEN 1 ELSE 0 END
For further info you should read the remarks section of SET ANSI_NULLS
Because you can't compare null values using comparison operators. You can only use is null or is not null or using functions like COALESCE(), ISNULL(). For example
SELECT CASE WHEN COALESCE(NULL,-1) IN (9,1,-1) THEN 1 ELSE 0 END
Null mean not defined.
NUll object is not equal to other NULL
NULL==1 return false
NUll==2 return false
NULL==NULL return also false

Oracle no results, small query

I am having trouble with some sql. When I run the following query:
Select * from teleapp;
I get TONS of results. Resulst which include a column (called cashwithappyn) that has TONS of empty or null data cells. (They look empty and don't say null)
The column info is:
ColumnName ID Null? Data Type Histogram Num Distinct Num Nulls Density
CASHWITHAPPYN 54 Y VARCHAR2(1 Byte) Frequency 2 56895 0
When I try to run the following query:
Select * from teleapp where cashwithappyn = null;
or
Select * from teleapp where cashwithappyn = '';
or
Select * from teleapp where cashwithappyn not like '';
or
Select * from teleapp where cashwithappyn not in ('Y','N');
or ANY type of combination, I cannot seem to get all of the rows with nothing in cashwithappyn.
Any ideas? Please help, this is the last part of a project that I was assigned to do and I just need to figure this out.
Thanks.
Maybe the column contains blanks. In that case you can do
WHERE TRIM(CASHWITHAPYYN) IS NULL
TRIM removes all blanks before and after and if nothing is left anymore the value becomes NULL
e.g.
TRIM(' ') IS NULL -- one blank removed = true
TRIM(NULL) IS NULL -- true
Also NULL cannot be compared with = NULL but must be phrased IS NULL. NULL is not a value as such and that is why the comparison never works.
You need to use IS NULL
Select * from teleapp where cashwithappyn is null;
Logical test expressions (=, Not In, Like etc) with null result in a false so all of the following result in false
1 = NULL
1 <> NULL
NULL = NULL
NULL <> NULL
NULL NOT IN ('a','b')
NULL Not Like NULL
Additionally in oracle the zero length string is null so NOT LIKE '' will never return any rows
You'll need to use either is null, is not null or Coalesce
A co-worker and I did a bunch of research, here's what we came up with that will work. Any ideas why this works but not the others?
Select * from teleapp where nvl(cashwithappyn,'U') = 'U';