One Line Solution, Fast and Succinct
\nSELECT id, IF((p_id IS NULL), \'Root\', IF(id IN (SELECT p_id FROM Tree), \'Inner\', \'Leaf\')) AS type FROM Tree\n\n\nIF((p_id IS NULL) : First, categorize e
```\nSELECT id, IF((p_id IS NULL), \'Root\', IF(id IN (SELECT p_id FROM Tree), \'Inner\', \'Leaf\')) AS type FROM Tree\n```\n\n``` IF((p_id IS NULL) ``` : First, categorize every case that has "Null" in its \'p_id\' as Root, as it has no upper branch.\n``` IF(id IN (SELECT p_id FROM Tree) ``` : For the rest of the group which are not Roots, categorize any case that is root for any other branch (``` id IN (SELECT p_id FROM Tree) ```)as Inner. \nOtherwise, all of them are Leaf since they have root and they don\'t have their own leaf.
[]