Given that (type_id, group_id) is the PK of widgets it is possible to avoid self-join: select group_id from widgets where type_id = A or type_id = B group by group_id having count(1) = 2;
pgsql-general by date:
Соглашаюсь с условиями обработки персональных данных