CS FundamentalsVery hardGATE CS

E(id, dept) = (1, 10), (2, 10), (3, 20), (4, 30), (5, NULL), (6, 20), (7, 40) and D(did, loc) = (10, X), (20, Y), (20, X), (30, Y), (50, X). Let Q1 = SELECT * FROM E LEFT JOIN D ON E.dept = D.did AND D.loc = 'X' and Q2 = SELECT * FROM E LEFT JOIN D ON E.dept = D.did WHERE D.loc = 'X'. How many rows do Q1 and Q2 return, respectively?

  1. A. 7 and 4
  2. B. 4 and 7
  3. C. 9 and 4
  4. D. 7 and 9

Correct answer: A. 7 and 4

A condition in ON only decides which D rows match, and unmatched E rows are still kept with NULLs (Q1 = 7); the same condition in WHERE removes the NULL-padded rows after the join (Q2 = 4). Plain LEFT JOIN would give 9 rows.

More DBMS and SQL questions

Exams that ask this topic