The following query will give you what is expected. select from @table A where ParentSID = 1 OR ParentSID IN (select SID from @table A where ParentSID = 1 AND FID - NULL)
I illustrated this in an example
Example
declare @table table ([SID] int, FID int, ParentSID int) insert into @table Values(1, null, null) insert into @table Values(2, null, null) insert into @table Values(3, 16, 1) insert into @table Values(4, 17, 1) insert into @table Values(5, null, 1) insert into @table Values(6, 18, 5) insert into @table Values(7, null, 1) insert into @table Values(8, 19, 7) select * from @table A where ParentSID=1 OR ParentSID IN (select SID from @table A where ParentSID=1 AND FID is NULL)
source share