Hi GauravHere's one way which should be easy to understand...--preparationdeclare @users table (Userkey int, Username varchar(10), Manager varchar(10))insert @users select 1, 'ABC', 'DEF'union all select 2, 'XYZ', 'ABC'union all select 3, 'PQR', 'ABC'declare @accounts table (Userkey int, accounts varchar(10))insert @accounts select 3, 'Test1'--queryselect * from ( select a.*, case when b.Userkey is null then 0 else 1 end as HasAccount, case when c.Manager is null then 0 else 1 end as IsManager from @users a left outer join @accounts b on a.Userkey = b.Userkey left outer join (select distinct Manager from @users) c on a.Username = c.Manager) awhere HasAccount = 0--to get requirement 2, use add ' and IsManager = 0'
Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part.