Reputation: 2144
Can a WHERE clause return NULL instead of TRUE or FALSE? According to the exercise below it is possible, but i can't imagine an example that returns NULL, Is it really possible?
4. Which of the following values can NOT be returned after evaluation of WHERE clause
condition?
A. UNKNOWN
B. TRUE
C. FALSE
D. NULL
Answer: A. If the result of the condition in WHERE clause is not known, NULL is returned. In all
other scenarios, either TRUE or FALSE is returned.
Upvotes: 6
Views: 2498
Reputation: 325
Not even a NULL can be equal to NULL.
A Non-Technical aspect
If you ask two girls, how old they are? may be you would hear them to refuse to answer your question, Both girls are giving you NULL as age and this doesn't mean both have similar age. So there is nothing can be equal to null.
SELECT 0 IS NULL , 0 IS NOT NULL , '' IS NULL , '' IS NOT NULL, NULL != NULL, NULL = NULL, NULL != '', NULL = ''
Upvotes: 0
Reputation: 1102
In SQL, all logical operators evaluate to TRUE, FALSE, and UNKNOWN (Oracle docs) in MySQL UNKNOWN result calls NULL (MySQL docs).
According to oracle documentation:
"To test for nulls, use only the comparison conditions IS NULL and IS NOT NULL. If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN."
So only TRUE, FALSE, and UNKNOWN can be returned after evaluation.
About your question:
"Can a WHERE clause return NULL instead of TRUE or FALSE?"
Strictly speaking in Oracle - NO because the such result called UNKNOWN.
But in general the meaning of UNKNOWN and NULL is equivalent in this context and it is just a different name for the same thing.
So the example of SQL below (a.a >= all
) evaluated as UNKNOWN.
with table_a as (
select null as a from dual
union all
select 10 as a from dual
union all
select 5 as a from dual),
table_b as (
select null as a from dual
union all
select 10 as a from dual
union all
select 5 as a from dual)
select * from table_a a where a.a >= all(select a from table_b b)
Upvotes: 2
Reputation: 380
As to explain the reason, consider that the SQL language uses a three-value logic: TRUE
, FALSE
, and NULL
. Let's consider this Orders table,
If we run the following query it wont return rows for CPU and Monitor
SELECT * FROM Orders WHERE (qty < 1000 Or qty >= 1000)
In this case, for CPU and Monitor condition (qty < 1000 Or qty >= 1000)
returns neither TRUE
nor FALSE
. It returns NULL
. Because logically it is unknown. So, the result of the condition in WHERE
clause is unknown and it returned NULL.
You can consider this reference.
Upvotes: 0