🟧 MySQL 🟧 Simple Solution using UNION (+ Explanation)
Our solution is based on the UNION operator, which combines the result-set of two or more SELECT statements  (Source).\n\nIn our case, we need to write three SE
Our solution is based on the `UNION` operator, which combines the result-set of two or more `SELECT` statements  ([Source](https://www.w3schools.com/sql/sql_union.asp)).\n\nIn our case, we need to write **three `SELECT` queries**, with each query isolating one type of node. This is achieved by carefully constructing the conditions of each query\'s `WHERE` clause. Specifically, a node is classified as a:\n\n- **Root** if there is no parent id, i.e. `p_id IS NULL`,\n- **Inner** if there is a parent (`p_id IS NOT NULL`) and the node\'s `id` is some other node\'s parent (`id IN (SELECT p_id FROM Tree WHERE p_id IS NOT NULL)`), and\n- **Leaf** if there is a parent and the node\'s `id` is **not** some other node\'s parent.\n\nLastly, we need to join these three queries with **two `UNION` operators**.\n\n<br>\n\n```sql\nSELECT id, \'Root\' AS \'type\'\nFROM Tree\nWHERE p_id IS NULL\n\nUNION\n\nSELECT id, \'Inner\' AS \'type\'\nFROM Tree\nWHERE \n    p_id IS NOT NULL AND\n    id IN (SELECT p_id FROM Tree WHERE p_id IS NOT NULL)\n\nUNION\n\nSELECT id, \'Leaf\' AS \'type\'\nFROM Tree\nWHERE\n    p_id IS NOT NULL AND\n    id NOT IN (SELECT p_id FROM Tree WHERE p_id IS NOT NULL)\nORDER BY id ASC;\n```
[]