Here are two alternatives:with cte as (select a.playerid ,a.playername ,count(b.teamid) as teamcount from table1 as a left outer join table2 as b on b.playerid=a.playerid group by a.playerid ,a.playername )select a.playername ,a.teamcount from cte as a inner join (select teamcount from cte group by teamcount having count(*)>1 ) as b on b.teamcount=a.teamcount order by a.teamcount ,a.playername
which givesplayername teamcountGarnett 0Perkins 0Allen 2Peirce 2
orwith cte as (select a.playerid ,a.playername ,count(b.teamid) as teamcount from table1 as a left outer join table2 as b on b.playerid=a.playerid group by a.playerid ,a.playername )select stuff((select ','+b.playername from cte as b where b.teamcount=a.teamcount for xml path('') ),1,1,'' ) as players ,a.teamcount from cte as a group by a.teamcount having count(*)>1 order by a.teamcount
which givesplayers teamcountGarnett,Perkins 0Allen,Peirce 2