Simple clean CASE expression explained. Faster than 96.95.%
CODE:\n\nSELECT id,\n    CASE\n        WHEN p_id IS null THEN \'Root\'                   # Check 1\n        WHEN id IN (SELECT p_id FROM tree) THEN \'Inner\' # 
**CODE:**\n```\nSELECT id,\n    CASE\n        WHEN p_id IS null THEN \'Root\'                   # Check 1\n        WHEN id IN (SELECT p_id FROM tree) THEN \'Inner\' # Check 2\n        ELSE \'Leaf\'                                     # Check 3\n    END as type\nFROM tree;\n```\n\n**EXPLINATION:**\nThis solution is returning two columns. The _`id`_ column and a newly created _`type`_ column. The CASE expression is used to fill in the values of the _`type`_ for each _`id`_.\n\nWhen this query runs, it starts at _`id`_ 1 and incrementally _"loops"_ through each _`id `_ until it reaches the end of the column. We use these incremental steps to check difference possibilities and determine the values of the _`type`_ column one by one.\n\n`Check 1`: This step is only needed **once** for the _`Root`_ and will **always** be the first check to be true.\n`Check 2`: In this step, we are checking if the node\'s id appears in the parent id column. If it does that means it has a child. Note that every node  _(besides the `Root`)_ with a child is always an `Inner`.\n`Check 3`: If the node is not the _`Root`_ and is not an _`Inner`_ then it has to be a `Leaf`. No conditional statement is needed.
['MySQL']