* A DAG 0 / | \ 7 8 9 \ /\ /\ \/ \/ \ 6 5 \ \/ \ \ 4 3 10 \/ \/ 1 2 -- Each row in the table represents an edge in the graph insert into hq_tab(child_id, parent_id) values(1,3); insert into hq_tab(child_id, parent_id) values(1,4); insert into hq_tab(child_id, parent_id) values(2,3); insert into hq_tab(child_id, parent_id) values(2,10); insert into hq_tab(child_id, parent_id) values(3,5); insert into hq_tab(child_id, parent_id) values(4,5); insert into hq_tab(child_id, parent_id) values(4,6); insert into hq_tab(child_id, parent_id) values(10,9); insert into hq_tab(child_id, parent_id) values(6,7); insert into hq_tab(child_id, parent_id) values(6,8); insert into hq_tab(child_id, parent_id) values(5,8); insert into hq_tab(child_id, parent_id) values(5,9); insert into hq_tab(child_id, parent_id) values(7,0); insert into hq_tab(child_id, parent_id) values(8,0); insert into hq_tab(child_id, parent_id) values(9,0); insert into hq_tab(child_id, parent_id) values(0,null); ** Going down the graph -- Give me the id structure in a way that the "child_id" of the previous row -- is the parent_id of current row, starting from the row whose child_id=8 -- What's important is the connect by clause. It defines the direction with -- which you walk the graph. Start with just gives you a strating point. -- You can use child_id or parent_id with "start with", as long as it is -- the node that you want. SQL> select * from hq_tab 2 start with child_id=8 3 connect by prior child_id=parent_id; PARENT_ID CHILD_ID ---------- ---------- 0 8 8 6 6 4 4 1 8 5 5 3 3 1 3 2 5 4 4 1 10 rows selected. -- Note that "edge" (4,1) was listed twice, since it is accessible to 2 paths. -- Note that child_id 1 was listed 3 times, since there are 3 possible paths -- of reaching that "node". SQL> SQL> select * from hq_tab 2 start with parent_id=5 3 connect by prior child_id=parent_id; PARENT_ID CHILD_ID ---------- ---------- 5 3 3 1 3 2 5 4 4 1 SQL> ** Going up the graph SQL> select * from hq_tab 2 start with child_id=1 3 connect by prior parent_id=child_id; PARENT_ID CHILD_ID ---------- ---------- 3 1 5 3 8 5 0 8 0 9 5 0 9 0 4 1 5 4 8 5 0 8 0 9 5 0 9 0 6 4 7 6 0 7 0 8 6 0 8 0 23 rows selected. SQL> -- Note the repeated edges and repeated parent nodes. * A tree 1 /\ 2 3 /\ \ 4 5 6 /\ 7 8 insert into hq_tab(child_id, parent_id) values(7,4); insert into hq_tab(child_id, parent_id) values(8,4); insert into hq_tab(child_id, parent_id) values(4,2); insert into hq_tab(child_id, parent_id) values(5,2); insert into hq_tab(child_id, parent_id) values(2,1); insert into hq_tab(child_id, parent_id) values(6,3); insert into hq_tab(child_id, parent_id) values(3,1); insert into hq_tab(child_id, parent_id) values(1,null); -- Walking down the tree SQL> select * from hq_tab 2 start with child_id=1 3 connect by prior child_id=parent_id; PARENT_ID CHILD_ID ---------- ---------- 1 1 2 2 4 4 7 4 8 2 5 1 3 3 6 8 rows selected. SQL> -- Entire graph walked, no edge repeated, no child node repeated. -- Walking up the tree SQL> select * from hq_tab 2 start with child_id=7 3 connect by prior parent_id=child_id; PARENT_ID CHILD_ID ---------- ---------- 4 7 2 4 1 2 1 SQL> -- Entire graph walked, no edge repeated, no parent node repeated.