CS FundamentalsHardAmazon/Flipkart SQL round · GATE CS

Table P(x) rows: (1), (2), (2), (3), (NULL). Table Q(x) rows: (2), (3), (3), (NULL), (4). What does this query return?

Table P(x) rows: (1), (2), (2), (3), (NULL). Table Q(x) rows: (2), (3), (3), (NULL), (4). What does this query return?
SELECT COUNT(*) FROM P
WHERE NOT EXISTS (SELECT * FROM Q WHERE Q.x = P.x);
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 0

Correct answer: B. 2

NOT EXISTS is two-valued: P's 1 has no match and P's NULL matches nothing (NULL = anything is unknown), so both rows qualify: 2. Contrast with NOT IN, which returns 0 here.

More DBMS and SQL questions

Exams that ask this topic