Are Conditional subquery
- by Tobias Schulte
I have a table foo and a table bar, where each foo might have a bar (and a bar might belong to multiple foos).
Now I need to select all foos with a bar. My sql looks like this
SELECT * FROM foo f WHERE [...] AND ($param IS NULL OR
(SELECT ((COUNT(*))>0) FROM bar b WHERE f.bar = b.id))
with $param being replaced at runtime.
The question is: Will the subquery be executed even if param is null, or will the dbms optimize the subquery out?