Two Simple SQL Solutions || Unions vs. Case
Solution 1. Uisng a Series of Unions\n\nSELECT id, "Root" as type FROM Tree\nWHERE p_id IS NULL\nUNION\nSELECT DISTINCT id, "Inner" as type FROM Tree\nWHERE p_i
**Solution 1. Uisng a Series of Unions**\n```\nSELECT id, "Root" as type FROM Tree\nWHERE p_id IS NULL\nUNION\nSELECT DISTINCT id, "Inner" as type FROM Tree\nWHERE p_id IS NOT NULL AND id IN (SELECT p_id FROM TREE)\nUNION\nSELECT DISTINCT id, "Leaf" as type FROM Tree\nWHERE p_id IS NOT NULL AND id not in(select p_id from tree where p_id is not null)\nORDER BY id\n```\n**Solution 2. Using Case**\nNote this is the simplier solution\n```\nSELECT id,\nCASE\n    WHEN p_id IS NULL THEN "Root"\n    WHEN id IN (SELECT p_id FROM Tree) THEN "Inner"\n    ELSE "Leaf"\n    END AS type \nFROM Tree\n```\n**Upvote if this was helpful!**
['Union Find', 'MySQL']