Let\'s say you have the following table:
items(item_id, item_parent)
... and it is a self-referencing table - item_parent
r
Oracle has a very convenient syntax for retrieving hierarchical data like this:
select
item_id,
item_parent,
level as depth
from
items
connect by
prior item_id = item_parent
start with
item_parent not in (select item_id from items)
This starts with the root nodes of your trees as those items whose item_parent does not exist in the table as item_id, and selects all children of those nodes, along with their depth in the tree.