Leetcode SQL (#607 – #610)

607. Sales Person

Question:

https://leetcode.com/problems/sales-person/

Solution:

select 
name
from salesperson 
where name not in (
    select p.name
    from orders o
        right join salesperson p 
            on o.sales_id = p.sales_id
    where o.com_id in (
        select distinct 
        com_id 
        from company 
        where name = 'RED'
    )    
    and o.amount >0 
    and o.amount is not null
)

608. Tree Node

Question:

https://leetcode.com/problems/tree-node/

Solution:

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

610. Triangle Judgement

Question:

https://leetcode.com/problems/triangle-judgement/

Solution:

select 
x,
y,
z,
case 
    when x+y > z and x+z > y and y+z > x then 'Yes'
    else 'No'
end as triangle
from triangle