create table #table ( id int, parrentid int)
insert into #table (id, parrentid)
values
(1 , 1),
(2 , 2),
(3 , 2),
(4 , 3),
(5 , 3),
(6 , 4),
(7 , 5),
(8 , 1),
(9 , 1 )
select * from #TABLE T
left join #TABLE T2 ON T.ID=T2.parrentid
where T2.id is null