Case When solution
\n\nselect id,\ncase \n    when p_id is null then \'Root\'\n    when p_id is not null and id IN (select p_id from Tree) then \'Inner\'\n    else \'Leaf\'\nEnd a
\n\nselect id,\ncase \n    when p_id is null then \'Root\'\n    when p_id is not null and id IN (select p_id from Tree) then \'Inner\'\n    else \'Leaf\'\nEnd as type\nfrom Tree
[]