Вы можете использовать агрегацию:
select ref_id
from mytable
where attr_name in ('f_name', 'l_name')
group by ref_id
having
max(attr_name = 'f_name' and attr_value = 'Sam') = 1
and max(attr_name = 'l_name' and attr_value = 'Border') = 1
Вот еще один способ express это (при условии отсутствия дубликатов (ref_id, attr_name)
):
select ref_id
from mytable
where (attr_name, attr_value) in (('f_name', 'Sam'), ('l_name', 'Boder'))
group by ref_id
having count(*) = 2