edited by
1,163 views
3 votes
3 votes

Use the following tables for the below queries wherever table $1$ and table $2$ are used:

Select *

From Table 1

Where not exists (Select $T_{2}B$ From Table 2 where $T_{2}B < > T_{1}B$)

How many number of rows will be there in the output ?

  1. $1$
  2. $2$
  3. $3$
  4. $0$

edited by

2 Answers

Best answer
2 votes
2 votes

0 will be the right answer.


Select T2B from T2 where T2B <> T1B

This inner sub query is returning column T2B of all the tuples from the table formed by cross product T1 x T2  where columns T1B &  T2B are not equal.

Table T1 has 3 tuples, & T2 has 2 tuples so T1xT2 will have 2x3 = 6 tupes.

In these 6 tuples values of attributes (T1B, T2B) will be as follows:

(aa, aa)

(aa, bb)

(bb, aa)

(bb, bb)

(cc, aa)

(cc, bb).

Out of these 6 (T1B, T2B) pairs 4 will be returned where T1B & T2B are not equal.

Since the sub query is has 4 tuples in it, the Not Exists construct will return the value false for each of the 3 tuples in T1.

So 0 tuples will be there in output.

2 votes
2 votes

WRONG ANSWER : Miscomputed the sub query.

Since every element in column T2B of table T2 is also present in the Column T1B of table T1, the sub query in the "Where" clause is going to return NULL. //This is incorrect.

The condition "Where Not Exists NULL" is a tautology,

that is this condition will always be true for every tuple in Table 1, so all the 3 tuples of table T1 will be shown in output.

Hence 3 should be the correct answer.


Why the condition "Where Not Exists NULL" is a tautology? 

Not Exists basically checks if the sub query returns any tuples or not & since the sub query is returning NULL, Where Not Exists NULL will always be evaluated to true.

Related questions

0 votes
0 votes
1 answer
2
shikharV asked Dec 8, 2015
398 views
Given answer: BI don't know how to deal with NULL in such query operations. Please explain.
0 votes
0 votes
1 answer
3
shikharV asked Dec 8, 2015
1,052 views
Given answer: BI don't know how to deal with NULL in such query operations. Please explain.