CS FundamentalsHardTCS Digital · Infosys SP · 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). Table Course(cid, credits) rows: ('C1', 4), ('C2', 3), ('C3', 2), ('C4', 3). What does this query 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). Table Course(cid, credits) rows: ('C1', 4), ('C2', 3), ('C3', 2), ('C4', 3). What does this query return?
SELECT s.sname, SUM(c.credits)
FROM Stu s JOIN Takes t ON s.sid = t.sid
          JOIN Course c ON c.cid = t.cid
GROUP BY s.sid, s.sname
ORDER BY s.sid;
  1. A. (Om, 7), (Pia, 4), (Raj, 3)
  2. B. (Om, 7), (Pia, 4), (Raj, 5), (Sia, 0)
  3. C. (Om, 4), (Pia, 4), (Raj, 5)
  4. D. (Om, 7), (Pia, 4), (Raj, 5)

Correct answer: D. (Om, 7), (Pia, 4), (Raj, 5)

Om took C1 (4) and C2 (3) = 7 — his NULL marks don't matter to credits; Pia C1 = 4; Raj C2 + C3 = 5. Sia has no Takes row and an inner join drops her.

More DBMS and SQL questions

Exams that ask this topic