My Table has two columns, no primary key defined.
say name and salary
GROUP BY. Use
HAVING to make sure a salary has two different names, and also no other than A and B:
select salary from tablename group by salary having count(distinct name) = 2 and count(case when name not in ('A','B') then 1 end) = 0