CS FundamentalsVery hardGATE CS (PYQ pattern)

Edge table G(a, b): (1,2), (2,3), (3,1), (3,4), (4,5), (6,7). The recursive query WITH RECURSIVE R(x,y) AS (SELECT a,b FROM G UNION SELECT R.x, G.b FROM R JOIN G ON R.y = G.a) SELECT COUNT(*) FROM R counts all reachable pairs (x, y) in the transitive closure. What does it return?

  1. A. 15
  2. B. 17
  3. C. 20
  4. D. 6

Correct answer: B. 17

From 1, 2 and 3 (which form a cycle) one reaches 1, 2, 3, 4, 5 (5 pairs each = 15); from 4: only 5 (1 pair); from 6: only 7 (1 pair); plus nothing from 5 or 7. UNION removes duplicates, so the cycle terminates. Total 17.

More DBMS and SQL questions

Exams that ask this topic