Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumSQLrecursive CTEsSingle-choice MCQ

This recursive CTE walks a parent/child tree but the data has a cycle (A->B->A). Without a guard, what happens, and what is the standard fix?

WITH RECURSIVE walk AS ( SELECT id, parent_id, ARRAY[id] AS path FROM node WHERE id = 'A' UNION ALL SELECT n.id, n.parent_id, w.path || n.id FROM node n JOIN walk w ON n.parent_id = w.id ) SELECT * FROM walk;