CS FundamentalsHardGATE CS · SQL

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 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). What does this query return?
SELECT s.sname, COUNT(*), COUNT(t.cid)
FROM Stu s LEFT JOIN Takes t ON s.sid = t.sid
WHERE s.sname = 'Sia'
GROUP BY s.sname;
  1. A. Sia, 1, 0
  2. B. No rows (empty result)
  3. C. Sia, 1, 1
  4. D. Sia, 0, 0

Correct answer: A. Sia, 1, 0

Sia has no Takes rows, so the LEFT JOIN keeps one row padded with NULLs. COUNT(*) counts that row (1); COUNT(t.cid) skips the NULL (0).

More DBMS and SQL questions

Exams that ask this topic