Back to problems

Classify Tree Nodes in SQL

SQL · Oracle · Medium

Given a Tree table that stores one record for each node in a tree, write a PostgreSQL query that labels every node as Root, Inner, or Leaf. Column descriptions: id integer, always present and unique. pid integer, nullable; when not NULL, it contains the id of the node's parent. A row whose pid value equals a node's id is that node's child. The rows always represent a valid, nonempty tree. Labeling rules: Root: pid is NULL. Inner: pid is not NULL, and this node appears as a…

Checking your access…