Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > create table tbl (fld1 text, fld2 text); > insert into tbl values > ('abc', 'D123'), > ('xyz', 'D123'), > ('abc', 'D125'), > ('abc', 'D128'), > ('def', 'D128'), > ('abc', 'D789'); > > <pre> > ✓ > 6 rows affected > </pre> <!-- --> > select > t1.* > from > tbl t1 > where > fld1 = 'abc' > and exists (select 1 > from tbl t2 > where t2.fld2 = t1.fld2 > group by fld2 > having count(*) = 1); > > <pre> > fld1 | fld2 > :--- | :--- > abc | D125 > abc | D789 > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=53977521cc06877747f60db1326702e6)*
back to fiddle