retagged by
790 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??

retagged by

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

Related questions

0 votes
0 votes
0 answers
2
Beyonder asked Nov 30, 2016
726 views
Can Anyone Explain how Query 28 works with example.
0 votes
0 votes
1 answer
3
aditya kuppa 1 asked Dec 5, 2017
1,089 views
How many minimum tables are required for this er diagram consisting of a many - many relation and total participation of one of the entities.? Can't i merge relation R an...
2 votes
2 votes
2 answers
4
indrajeet asked Sep 23, 2016
3,328 views
Consider the following entity relation diagramIf A has 50 entities,B has 20 entities and C has 200 entities. what is the maximum number of triples of entities that could ...