CS FundamentalsHardProduct placement · SQL round

Table Node(id, parent) rows: (1, NULL), (2, 1), (3, 1), (4, 2), (5, 2), (6, 4), (7, 3). What does this query return?

Table Node(id, parent) rows: (1, NULL), (2, 1), (3, 1), (4, 2), (5, 2), (6, 4), (7, 3). What does this query return?
WITH RECURSIVE sub(id, depth) AS (
  SELECT id, 0 FROM Node WHERE id = 2
  UNION ALL
  SELECT n.id, s.depth + 1
  FROM Node n JOIN sub s ON n.parent = s.id)
SELECT COUNT(*), MAX(depth) FROM sub;
  1. A. 3, 2
  2. B. 4, 3
  3. C. 4, 2
  4. D. 7, 3

Correct answer: C. 4, 2

Starting at 2 and walking downwards: 2 (depth 0), its children 4 and 5 (depth 1), and 4's child 6 (depth 2). The anchor row is included, so 4 rows.

More DBMS and SQL questions

Exams that ask this topic