Short Query with Clear Explanation and Diagram
\n\nSELECT id, IF(p_id IS NULL, \'Root\', IF(id IN (SELECT p_id FROM TREE), \'Inner\', \'Leaf\')) AS type FROM Tree;\n\n\nExplanation: It is important to solve 
![image](https://assets.leetcode.com/users/images/26524e6f-7084-4089-93a8-227057850190_1656600311.6161857.jpeg)\n```\nSELECT id, IF(p_id IS NULL, \'Root\', IF(id IN (SELECT p_id FROM TREE), \'Inner\', \'Leaf\')) AS type FROM Tree;\n```\n\nExplanation: It is important to solve the question by thinking about the possible scenarios in every record/row of the database. The value in the ```type``` column is determined by both the```p_id``` and ```id``` of a record/row using one ```if``` condition nested in another ```if``` condition. \n\nThe IF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.\n\n```sql\nIF(condition, value_if_true, value_if_false)\n```\n\nThe outer ```if``` condition tests if the record\'s ```p_id``` is null. If it is, the record\'s ```type``` must be Root. If it is not, whether the record\'s ```type``` is Inner or Leaf is determined by the inner ```if``` condition. \nThe inner ```if``` condition tests if the record\'s ```id``` is in the column ```p_id```. If it is, it means that the record/row is the parent node of other records/rows, hence it is an Inner. If it is not, the record/row is a Leaf.\n
['MySQL']