CS FundamentalsHardGATE CS · Amazon/Microsoft SDE MCQ

Table Emp(eid, name, mgr, dept, sal) rows: (1, 'Aru', NULL, 'D1', 90), (2, 'Bin', 1, 'D1', 60), (3, 'Cy', 1, 'D2', 70), (4, 'Dev', 2, 'D1', 60), (5, 'Eli', 2, 'D2', 40), (6, 'Fay', 3, NULL, 75), (7, 'Gus', 4, 'D1', 30). mgr refers to eid. What does this query return? WITH RECURSIVE r(id) AS ( SELECT eid FROM Emp WHERE mgr = 2 UNION ALL SELECT e.eid FROM Emp e JOIN r ON e.mgr = r.id) SELECT COUNT(*) FROM r;

  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 5

Correct answer: B. 3

The anchor gives Bin's direct reports Dev (4) and Eli (5); the next step adds Gus (7), who reports to Dev; nobody reports to Gus or Eli. Bin himself is not in the anchor, so the count is 3.

More DBMS and SQL questions

Exams that ask this topic