1Z0-047 · Question #204
Which two statements are true regarding the EXISTS operator used in the correlated subqueries? (Choose two.)
The correct answer is A. The outer query stops evaluating the result set of the inner query when the first value is found. C. It is used to test whether the values retrieved by the outer query exist in the result set of the inner query. See the full explanation below for the reasoning.
Question
Which two statements are true regarding the EXISTS operator used in the correlated subqueries? (Choose two.)
Options
- AThe outer query stops evaluating the result set of the inner query when the first value is found.
- BIt is used to test whether the values retrieved by the inner query exist in the result of the outer query.
- CIt is used to test whether the values retrieved by the outer query exist in the result set of the inner query.
- DThe outer query continues evaluating the result set of the inner query until all the values in the result set
How the community answered
(51 responses)- A80% (41)
- B8% (4)
- D12% (6)
Community Discussion
4A and C are correct. EXISTS tests whether the outer query's current row produces any matching rows in the inner query (that is the direction, outer-to-inner, which eliminates B), and the moment the inner query finds one match it stops searching, which is the short-circuit behavior in A and the exact reason EXISTS is faster than IN on large result sets, so both of those facts are worth a dedicated card with the word "short-circuit" on the back.
Quick win here, do not overthink it. EXISTS fires true the instant the inner query finds one matching row, so the outer query bails out early and moves on, which is why A is solid. And the whole point of EXISTS is that you feed it a row from the outer query and ask "does anything in here match this?" which makes C the natural pick over B, because B has the direction flipped. My question for you: can you explain in your own words why flipping that direction, outer checking inner versus inner checking outer, changes the logic of what gets returned?
I almost circled B on reflex because the wording "values retrieved by the inner query exist in the result of the outer query" sounds plausible until you actually trace a correlated EXISTS at runtime and remember the direction of the test goes outer row drives the inner probe, which is exactly what C says. D is the trap for anyone who confuses EXISTS with IN, because the whole performance argument for EXISTS since Oracle 7 is that the engine short-circuits on the first hit, which is A.
The short-circuit point is worth drilling into a card of its own, because a lot of people know it as trivia but forget that it only matters when the inner query is not pre-materialized by the optimizer, which is the case where EXISTS and IN actually diverge in execution plans.