CS FundamentalsHardProduct-company placement · GATE CS

Cust(cid, city) has rows (1, 'Pune'), (2, 'Delhi'), (3, NULL), (4, 'Pune'). Ord(oid, cid, amt) has rows (10, 1, 500), (11, 1, NULL), (12, 2, 300), (13, NULL, 200), (14, 5, 100). How many rows do these queries return? Q1: SELECT * FROM Cust c LEFT JOIN Ord o ON c.cid = o.cid AND o.amt > 250; Q2: SELECT * FROM Cust c LEFT JOIN Ord o ON c.cid = o.cid WHERE o.amt > 250;

  1. A. Q1 = 4, Q2 = 2
  2. B. Q1 = 2, Q2 = 2
  3. C. Q1 = 5, Q2 = 2
  4. D. Q1 = 4, Q2 = 4

Correct answer: A. Q1 = 4, Q2 = 2

In Q1 the amount test is part of the join, so every customer survives: 1 pairs with order 10, 2 with 12, and 3 and 4 get NULLs (4 rows). In Q2 the WHERE runs after the join and drops every row whose o.amt is NULL or small, leaving 2: a WHERE on the right table turns the outer join into an inner join.

More DBMS and SQL questions

Exams that ask this topic