why return 'Inner' when using when id not in (select a.p_id from tree a) then 'Leaf'


  • 0
    C
    select id,
           (case
             when p_id is NULL then 'Root'
             when id not in (select a.p_id from tree a) then 'Leaf'
             else 'Inner'
            end) as Type
    from tree
    order by id
    

    I thought the code above would be better than using the following because 1 is also in the p_id but it is a root.
    case
    when p_id is NULL then 'Root'
    when id in (select a.p_id from tree a) then 'Inner'
    else 'Leaf'
    end


  • 0

    There you go:)

     select t1.id, 
     (case 
      when t1.p_id is null then 'Root'
      when t1.id not in (select t.p_id from tree t where t.p_id is not null) then 'Leaf'
      else 'Inner'
      end) as Type 
    from tree t1
    order by t1.id

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.