Easy Explanation for beginners
\nselect id,\'Root\' as type\nfrom tree\nwhere p_id is null\n\nunion\n\nselect id, \'Leaf\' as type\nfrom tree\nwhere id not in (\n    select distinct p_id\n   
```\nselect id,\'Root\' as type\nfrom tree\nwhere p_id is null\n\nunion\n\nselect id, \'Leaf\' as type\nfrom tree\nwhere id not in (\n    select distinct p_id\n    from tree\n    where p_id is not null\n) \nand p_id is not null\n\nunion\n\nselect id, \'Inner\' as type\nfrom tree\nwhere id in (\n    select distinct p_id\n    from tree\n    where p_id is not null\n) \nand p_id is not null\n\norder by id;\n```\n\nIt is a simple usecase of union,\nIf the node does not have a parent, then it is a root node.\nIf the node is a parent of any other node and has a parent node, then it is an inner node.\nIf the node is not a parent node of any other node and has a parent node, then it is a leaf node.\n\nWe check for each of the statements separately and then union them together\n\n
[]