create table #Account(AccountId int) insert into #Account select 1500722090 union select 1500722091 union select 1500722092 union select 1500722316 union select 1500722317
--'union' is auto filter duplication data
--'union all' is get all data select a.AccountId, sum(case when b.AccountId is not null then 1 else 0 end) as flag from #Account a left join dbo.AccountHolding b on a.AccountId=b.AccountId group by a.AccountId having sum(case when b.AccountId is not null then 1 else 0 end)=0