0
0
PostgreSQLquery~20 mins

Recursive CTE for graph traversal in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive CTE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find all reachable nodes from node 1
Given the table edges with columns source and target, what is the output of this recursive CTE query that finds all nodes reachable from node 1?
PostgreSQL
WITH RECURSIVE reachable AS (
  SELECT source, target FROM edges WHERE source = 1
  UNION
  SELECT e.source, e.target FROM edges e
  INNER JOIN reachable r ON e.source = r.target
)
SELECT DISTINCT target FROM reachable ORDER BY target;
A[2, 3]
B[1, 2, 3, 4]
C[2, 3, 4]
D[3, 4]
Attempts:
2 left
💡 Hint
Think about how the recursive part expands the reachable nodes starting from source 1.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in recursive CTE
Which option contains a syntax error in this recursive CTE that tries to find all nodes reachable from node 1?
PostgreSQL
WITH RECURSIVE reachable AS (
  SELECT source, target FROM edges WHERE source = 1
  UNION
  SELECT e.source, e.target FROM edges e
  JOIN reachable r ON e.source = r.target
)
SELECT DISTINCT target FROM reachable ORDER BY target;
ANo syntax error; query is valid
BMissing keyword RECURSIVE after WITH
CUsing JOIN instead of INNER JOIN causes syntax error
DMissing alias for recursive CTE
Attempts:
2 left
💡 Hint
Check if JOIN without INNER is valid in PostgreSQL.
optimization
advanced
2:00remaining
Optimize recursive CTE to avoid duplicates
Which option optimizes this recursive CTE to avoid revisiting nodes and duplicates in the result?
PostgreSQL
WITH RECURSIVE reachable(node) AS (
  SELECT 1
  UNION
  SELECT e.target FROM edges e
  JOIN reachable r ON e.source = r.node
)
SELECT node FROM reachable ORDER BY node;
AUse UNION ALL instead of UNION
BAdd WHERE e.target NOT IN (SELECT node FROM reachable) in recursive part
CAdd DISTINCT in the final SELECT
DAdd a LIMIT clause to restrict recursion depth
Attempts:
2 left
💡 Hint
Prevent revisiting nodes by filtering them out in recursion.
🧠 Conceptual
advanced
2:00remaining
Understanding recursive CTE termination
What causes a recursive CTE for graph traversal to stop recursing?
AWhen no new rows are added in the recursive part
BWhen a maximum recursion depth is reached
CWhen the base case returns no rows
DWhen the query uses UNION ALL instead of UNION
Attempts:
2 left
💡 Hint
Think about how recursion ends naturally in SQL.
🔧 Debug
expert
3:00remaining
Diagnose infinite recursion in recursive CTE
Given this recursive CTE, why does it cause infinite recursion or run indefinitely?
PostgreSQL
WITH RECURSIVE reachable(node) AS (
  SELECT 1
  UNION ALL
  SELECT e.target FROM edges e
  JOIN reachable r ON e.source = r.node
)
SELECT node FROM reachable ORDER BY node;
AMissing ORDER BY in recursive part causes infinite recursion
BBase case selects node 1 incorrectly
CUsing UNION instead of UNION ALL causes infinite recursion
DEdges contain cycles and no check prevents revisiting nodes
Attempts:
2 left
💡 Hint
Consider what happens if the graph has loops.