I am trying to identify the "Leaf" in a tree but I am confused why my query is not giving me what I want.
Here is the Problem:
So my idea is that as long as the id is not in p_id column, then it should be a "Leaf"
select id, 'Leaf' as type
from Tree
where id not in (select distinct(p_id)
from Tree)
However, the query above is returning nothing to me.
The solution is pretty much the same as mine except it specify that the p_id cannot be NULL, then it is returning what I want.
select id
from Tree
where id not in(select distinct(p_id)
from Tree
where p_id IS NOT NULL)
I am confused as why does adding the where clause will make a difference?
