Checking tables for null returns 0 regardless - sql

I don't understand why this query doesn't work.
I have a table which is full of rows where 3 of the column values are set to NULL.
But when I run the following query, it returns 0 (it should return 96)
SELECT COUNT(*) FROM SEAT WHERE BOOKED=null;
Do you know what I am doing wrong?

It depends on your database settings and the specific RDBMS you are using, but if you are using ANSI NULL syntax you cannot directly compare a value to NULL with an equality test (=) -- it will always fail.
Use WHERE BOOKED IS NULL instead.

You have to use IS NULL instead of = null
Since null technically isn't a value, you can't compare null using the = operator.

Use IS NULL
SELECT COUNT(*)
FROM SEAT
WHERE BOOKED IS NULL

Related

Null query result cant fetch in Select query in Oracle

I have below query which i am trying to run but not returning the expected result. The ISIN field value which is Null in EXPORT_BB is also getting ignore and not showing the result with the condition given in NOT IN clause. The export_blacklist has only one row value and which is not Null but still i dont for what reason the null value is getting ignored.
Select * from EXPORT_BB where ISIN NOT IN
(
SELECT
ISIN
FROM
export_blacklist);
If i run only select query without the NOT IN clause then i can see values which is NULL for ISIN field.
JUst for test i tried below query and its also resulting nothing. Is it bug in Oracle 18c or something is missing?
select 'null is not in set' from dual where null not in (select 1 from dual);
Any comparison of NULL with =, <>, <, > or in a IN or NOT IN clause will return NULL, so that row is not included in the results (because only rows for which the returned value is TRUE will be included in the results).
Change your code with a condition for the case that ISIN is NULL:
SELECT * FROM EXPORT_BB
WHERE ISIN NOT IN (SELECT ISIN FROM export_blacklist)
OR ISIN IS NULL
NULL values doesn't work with NOT IN it's the normal behaviour.
You have to convert the NULL to another value to be able to operate with it or use IS NULL/IS NOT NULL
Select * from EXPORT_BB where NVL(ISIN, 999999) NOT IN
(
SELECT
NVL(ISIN, 999999)
FROM
export_blacklist);
Comparing to a null value in Oracle always returns false.
Is NULL >= 1? No.
Is NULL < 1? No.
Is NULL in your set? Regardless of what your set is, the answer is no.
Is NULL not in your set? Again, no.
It is the expected behaviour. NOt related to 18c it is the same way from Oracle 7 onwards
NOT IN doesnt consider nulls.
NOT EXISTS does consider nulls.
Consider the following example in db fiddle
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=8be0a790d8172093a032602345038e8e
See a discussion on this
https://asktom.oracle.com/pls/apex/asktom.search?tag=in-vs-exists-and-not-in-vs-not-exists
As you have been answered by collegues you have to specify that you wanna return null values too.
Namely
SELECT *
FROM EXPORT_BB
WHERE ISIN NOT IN (SELECT ISIN FROM EXPORT_BLACKLIST)
OR ISIN IS NULL;

Not in clause on a column with Null values

I have a table with 10 odd columns, one of them being 'Status'.
I wanted to fetch all rows where Status is not Rejected, so I wrote the following query on Hive:
select * from table1 where status <> 'Rejected'
However Hive is not returning me rows where the Status was Null. I changed the query to
select * from table1 where status <> 'Rejected' or status is Null
But I can't find any documentation to understand why this is happening.
Can someone please help me with this?
Hive implements the NULL-safe comparison operator. So you can do:
select *
from table1
where not status <=> 'Rejected' ;
As for your question, it is a pretty basic question on what NULL means in databases. It doesn't mean "missing", it means "unknown". Almost all comparison operations return NULL when either operand is NULL -- the exceptions are operands (such as <=>, is not null and is null) that are specific designed to handle NULL values.
null isn't a value, it's the lack thereof. Whenever you try to use it in a context of the value, the result would be "unkonwn". You can think about it like this - "is an unknown (=null) value different from 'Rejected'? We don't know."
Thus, you need to specifically handle it with the is [not] operator. You can think of the second where clause you shared as "all the statuses that are not known to have the value 'Rejected'".

<> and != avoiding NULL in WHERE condition

I have a table PATIENT with Column STATUS. When I queried to get STATUS not equal to 1, I was expecting the result as NULL and 2.
But I am only getting 2 as the result. Can someone help me with this?
CREATE TABLE #PATIENT
(STATUS INT)
INSERT INTO #PATIENT (STATUS)
SELECT 1
UNION
SELECT 2
UNION
SELECT NULL
SELECT * FROM #PATIENT WHERE STATUS <> 1
When I queried with
SELECT * FROM #PATIENT WHERE ISNULL(STATUS, 0) != 1
I am able to get NULL and 2 as the result.
This is SQL SERVER 2012.
You can use OR in WHERE with Condition STATUS IS NULL .
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
This will do it.
EDIT:
Conceptually, NULL means “a missing unknown value” and it is
treated somewhat differently from other values.
You cannot use arithmetic comparison operators such as =, <, or <>
to test for NULL
Because the result of any arithmetic comparison with NULL is also
NULL, you cannot obtain any meaningful results from such comparisons
we can not equate or not equate anything with null, thats why IS NULL
SELECT NULL <> 1 ===> NULL
Even though it is supposed to be true, it will return `NULL`
Hope this helps.
When you compare NULL value to any value then the result is always NULL.
So if you wan to select the NULL value as well then try this:
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
DEMO
Conceptually "NULL" means a missing value. To test for NULL IS NULL or IS NOT NULL condition is used. Arithmetic operators cannot be used for comparing NULL values.
NULL<>1 :: returns false because NULL can be 1 or any other value (an unknown value).
Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database.
And reason because you got only 2 and NOT NULL is because in SQL nothing is equal to NULL but also nothing is NOT equal to NULL.
In Simple words you cannot equate and not equate any value with NULL.

How to display Items that equal a value in one column and are not null in another

Let's say I want to display all items in a table with following criteria how would i do this?
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 != 'NULL'
I want it to display all values from COLUMN1. How does one go about this process in MS SQL?
NULL values can be compared using IS [NOT] NULL in SQL server. Please check this.
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 IS NOT NULL
NULL is an UNKNOWN value , you cannot use any Comparison Operators (= , <> , > , <) with it. you check for nulls like
ColumnName IS NULL or ColumnName IS NOT NULL
If you think about it , it makes sense, to compare two or more values, you need to know the values only then you can compare them, Since SQL Server considers a NULL to be an UNKNOWN value, you cannot really compare an unknown value to anything.

mismatch not picked up when one value is null

I have a simple SQL query where a comparison is done between two tables for mismatching value.
Yesterday, we picked up an issue where one field was null and the other wasn't, but a mismatch was not detected.
As far as I can determine,the logic has been working all along until yesterday.
Here is the logic of the SQL:
CREATE TABLE Table1
(UserID INT,PlayDate DATETIME)
CREATE TABLE Table2
(UserID INT,PlayDate DATETIME)
INSERT INTO Table1 (UserID)
SELECT 5346
INSERT INTO Table2 (UserID,PlayDate)
SELECT 5346,'2012-11-01'
SELECT a.UserID
FROM Table1 a
INNER JOIN
Table2 b
ON a.UserID = b.UserID
WHERE a.PlayDate <> b.PlayDate
No values are returned even though the PlayDate values are different.
I have now updated the WHERE to read:
WHERE ISNULL(a.PlayDate,'') <> ISNULL(b.PlayDate,'')
Is there a setting in SQL which someone could have changed to cause the original code to no longer pick up the difference in fields?
Thanks
NULL <> anything
is unknown not true. SQL uses three valued logic (false/true/unknown) and the predicate needs to evaluate to true in a where clause for the row to be returned.
In fact in standard SQL any comparison with NULL except for IS [NOT] NULL yields unknown. Including WHERE NULL = NULL
You don't state RDBMS but if it supports IS DISTINCT FROM you could use that or if you are using MySQL it has a null safe equality operator <=> you could negate.
You say you think it previously behaved differently. If you are on SQL Server you might be using a different setting for ANSI_NULLS somehow but this setting is deprecated and you should rewrite any code that depends on it anyway.
You can simulate IS DISTINCT FROM in SQL Server with WHERE EXISTS (SELECT a.PlayDate EXCEPT SELECT b.PlayDate)
Not even a NULL can be equal to NULL.
Here are two common queries that just don’t work:
select * from table where column = null;
select * from table where column <> null;
there is no concept of equality or inequality, greater than or less
than with NULLs. Instead, one can only say “is” or “is not”
(without the word “equal”) when discussing NULLs.
- The correct way to write the queries
select * from table where column IS NULL;
select * from table where column IS NOT NULL;