CS FundamentalsHardGATE CS (PYQ pattern)

Table T(x) holds the values 1, 2, 3, NULL and table U(y) holds 2, NULL. Let Q1 = SELECT COUNT(*) FROM T WHERE x NOT IN (SELECT y FROM U); and Q2 = SELECT COUNT(*) FROM T WHERE NOT EXISTS (SELECT 1 FROM U WHERE U.y = T.x); The results of Q1 and Q2 are:

  1. A. 0 and 3
  2. B. 2 and 3
  3. C. 2 and 2
  4. D. 3 and 3

Correct answer: A. 0 and 3

x NOT IN (2, NULL) expands to x <> 2 AND x <> NULL, which is never TRUE (it is FALSE or UNKNOWN), so Q1 = 0. NOT EXISTS only checks for matching rows, and U.y = T.x matches only x = 2, so rows 1, 3 and NULL qualify: Q2 = 3.

More DBMS and SQL questions

Exams that ask this topic