Oracle has a proprietary family tree feature that allows you to traverse a hierarchy and a family-tree type of report with a single SQL query. This uses the connect by, start with, and prior features. Consider an explosion of a family tree from the root to the leaf levels: Our table will contain the columns parent_column and child_column. These will follow the relationship from great-grandparent to grandparent to parent to child, assuming that the same person can exist as both a parent and a child and that every person exists as a child (though not necessarily as a parent). We will start to build the tree from the oldest person, Adam, and will continue through the succeeding leaf levels in the following manner:
Select parent_column, child_column
>From Family_Tree
start with child_column = 'Adam'
connect by parent_column = PRIOR child_column;
If our table contains the following:
Parent_Column | Child_column |
Unknown | Adam |
Adam | Steve |
Adam | Bill |
Bill | Mary |
Steve | Helen |
Mary | Ann |
the result below will be returned, indented to show the effect of the hierarchy:
Parent_column child_column
Unknown Adam
Adam Steve
Steve Helen
Adam Bill
Bill Mary
Mary Ann This family tree does not need to be balanced. Notice the keyword Prior,
which connects a column in a new row to a column in the previously
selected row.