CS FundamentalsVery hardAmazon/Flipkart SQL round · GATE CS

Table Stu(sid, sname) rows: (1, 'Om'), (2, 'Pia'), (3, 'Raj'), (4, 'Sia'). Table Takes(sid, cid, marks) rows: (1, 'C1', 80), (1, 'C2', NULL), (2, 'C1', 65), (3, 'C2', 90), (3, 'C3', 75), (5, 'C1', 50). What counts do Query 1 and Query 2 return?

Table Stu(sid, sname) rows: (1, 'Om'), (2, 'Pia'), (3, 'Raj'), (4, 'Sia'). Table Takes(sid, cid, marks) rows: (1, 'C1', 80), (1, 'C2', NULL), (2, 'C1', 65), (3, 'C2', 90), (3, 'C3', 75), (5, 'C1', 50). What counts do Query 1 and Query 2 return?
-- Query 1
SELECT COUNT(*) FROM Stu s LEFT JOIN Takes t
  ON s.sid = t.sid WHERE t.marks >= 70;
-- Query 2
SELECT COUNT(*) FROM Stu s LEFT JOIN Takes t
  ON s.sid = t.sid AND t.marks >= 70;
  1. A. 3 and 3
  2. B. 5 and 3
  3. C. 3 and 5
  4. D. 5 and 5

Correct answer: C. 3 and 5

In Query 1 the WHERE runs after the outer join and drops every row whose marks are NULL or < 70, leaving 3. In Query 2 the condition is part of ON: Om matches 1 row, Raj 2, and Pia and Sia each keep one NULL-padded row, so 5.

More DBMS and SQL questions

Exams that ask this topic