in Databases retagged by
786 views
6 votes
6 votes

Consider the following table Mystery

Mystery
A B
1 Null
2 4
3 5
Null 6

select count(\ast) from Mystery
where A not in( select B from Mystery)

(A) 1

(B) 0

(C) 4

(D) 3

ans is B given

please explain how??

in Databases retagged by
786 views

1 Answer

5 votes
5 votes

This will give 0...because of null values...where will work like if it is true for that value then the row is returned or false it will not return the row with that value....on the otherhand null is a unknown value so when it is compared with true,false or unknown it return unknown itself so when there are null the not in operator cannot give the correct truth or false values...

 

on the other hand "in" will work perfectly fine

the query can be simplified like this 

select count(\ast) from Mystery 
where A not in( select B from Mystery)

this will evaluate as

select count(\ast) from Mystery 
where (1<>NUll and 1<>4 and 1<>5 and 1<>6) or (2<>NULL and 2<>4......)or (3<>Null.....) or (NULL<>NULL....)

so 1<>NUll--unknown and 1<>4--true and 1<>5--true and 1<>6--true

eventhough rest of them are true due to "and" of unknown the expression evaluates to unknown...

where will return particular row only when it is evaluated to true

edited by

4 Comments

wht will query rturn if it's in oprator
0
0
so what will be the answer in case of IN?
0
0

let take an example.....

..1<>NULL  ....actully this condition is unknown therefor it will result false for all condition....

means if you are checking for equal then it will think it may not be equal and if you are checking for not equal then it will think it may be equal.....means in both the cases it will result false........

0
0

Related questions

0 votes
0 votes
0 answers
2