I have three tables:
- Foo
- Bar
- FooBar
FooBar is a relation table that contains collection of Foos and Bars that are related to one another, it has only two columns (FooId, BarId).
My code so far for getting all the Foo that relate to all the Bars out:
select
f.*
from
Foo f
where
f.FooId IN
(
SELECT fb.FooId
FROM FooBar fb
GROUP BY fb.FooId
HAVING COUNT(*) = (SELECT COUNT(*) FROM Bar)
)
There has to be more efficient way to write this. I could put total number of Bars in a SQL variable outside of outer select so it doesn't execute every time, but that's the only optimization that I can think of so far.