✅ MySql | Easy to understand | 2 Approaches
Approach 1 using IF condition\n\nIn this approach first we will check if p_id is null then its a "Root" node.\n\nFurther in a nested if we will check if it has 
**Approach 1 using IF condition**\n\nIn this approach first we will check if p_id is null then its a "Root" node.\n\nFurther in a nested if we will check if it has id is also parent of some other node than its a "Inner".\n\nAt last if it does not satisfied both condition then it must be "Leaf".\n\n```\nselect id, IF(p_id is NULL, "Root", IF(id IN (select distinct p_id from Tree), "Inner", "Leaf")\n) as type from Tree\n```\n\n**Approach 2 using CASE**\n\nIn this approach we will check same condition but this time using CASE-WHEN keyword.\n\n```\nSELECT id, CASE\n    WHEN p_id IS NULL THEN \'Root\'\n    WHEN id NOT IN (SELECT DISTINCT(p_id) FROM tree WHERE p_id IS NOT NULL) THEN \'Leaf\'\n    ELSE \'Inner\'\nEND AS \'Type\'\nFROM Tree\nORDER BY id\n```
['MySQL']