CS FundamentalsHardGATE CS (PYQ pattern)

ORD(oid, amt): (1,100), (2,200), (3,300). LN(oid, item): (1,a), (1,b), (1,c), (2,a), (3,b), (3,c). PAY(oid, paid): (1,40), (1,60), (3,300). What do SELECT SUM(amt) FROM ORD JOIN LN ON ORD.oid = LN.oid JOIN PAY ON ORD.oid = PAY.oid and the same query with LEFT JOIN PAY instead of JOIN PAY return?

  1. A. 600 and 600
  2. B. 1200 and 1400
  3. C. 1100 and 1400
  4. D. 1200 and 1200

Correct answer: B. 1200 and 1400

Each order's amt is repeated once per (line, payment) combination. Inner joins: order 1 -> 3 x 2 = 6 rows (600), order 2 has no payment (dropped), order 3 -> 2 x 1 = 2 rows (600): 1200. With LEFT JOIN PAY, order 2 keeps its single line with a NULL payment: +200 = 1400. The true total of orders is 600.

More DBMS and SQL questions

Exams that ask this topic