Select a.*,b.productname
from TableA a
join TableB b on a.sortid = b.sortid
where b.id in (select top 3 id from tableB where sortid = b.sortid)