CS FundamentalsVery hardGATE CS

Ed(a, b) = (1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (4, 6), (5, 7), (6, 7), (2, 5) is a directed acyclic graph. The query WITH RECURSIVE R(n) AS (SELECT 1 UNION ALL SELECT b FROM Ed JOIN R ON Ed.a = R.n) SELECT COUNT(*) FROM R returns how many rows? (UNION ALL keeps duplicates; each row of R is one path from node 1.)

  1. A. 15
  2. B. 7
  3. C. 18
  4. D. 13

Correct answer: A. 15

With UNION ALL every distinct path from node 1 yields a row (paths to node 7 are numerous because the graph has diamonds), giving 15. With UNION the rows would be deduplicated to the 7 reachable nodes.

More DBMS and SQL questions

Exams that ask this topic