I've mocked up this table...
| r |
f1 |
f2 |
f3 |
| 1 |
A |
B |
val1 |
| 2 |
A |
B |
val2 |
| 3 |
A |
B |
val1 |
| 4 |
A |
E |
val1 |
| 5 |
A |
E |
val2 |
| 6 |
A |
G |
val1 |
| 7 |
A |
G |
val1 |
...and I assume you want to find all combinations of (f1,f2) which have 'val1' or 'val2' in f3.
Thus, from that table, you would expect to get the combinations (A,B) and (A,E) but not (A,G) because the last combination doesn't feature 'val2'.
First, get unique combinations having either 'val1' or 'val2' in f3.
SELECT DISTINCT f1,f2,f3 FROM table1 WHERE f3 IN ('val1','val2')
Once you have that, then group the rows on the first two columns and count how many of each combination you have. You want combinations where there are two (which must be both 'val1' and 'val2'):
SELECT f1,f2 FROM
(
SELECT DISTINCT f1,f2,f3 FROM table1 WHERE f3 IN ('val1','val2')
) q
GROUP BY f1,f2
HAVING COUNT(f3)=2
This will get you
With nested queries this isn't going to be very efficient. Optimising depends at least to some extent on the amount of data, its variability and which database you're using.