Difficulty: Easy
Correct Answer: Correct
Explanation:
Introduction / Context:
EXISTS is a core SQL predicate used to test for the presence of rows from a subquery. Understanding how EXISTS short-circuits and how it differs from IN and JOIN patterns is crucial for writing efficient, semantically correct queries.
Given Data / Assumptions:
Concept / Approach:
EXISTS is evaluated as a semi-join test. The database engine stops scanning the subquery as soon as a single qualifying row is found. This makes it ideal for presence checks, especially with correlated subqueries and appropriate indexes.
Step-by-Step Solution:
Write a subquery expressing the matching condition.Wrap it in EXISTS (SELECT 1 FROM ... WHERE correlation).Rely on short-circuit behavior to avoid unnecessary work.Use NOT EXISTS for anti-join semantics (finding non-matching rows).Compare behavior to IN and LEFT JOIN ... IS NULL for clarity and performance.
Verification / Alternative check:
Test with sample data: add or remove a single qualifying row and observe the Boolean flip of EXISTS.
Why Other Options Are Wrong:
EXISTS does not require exactly one row or GROUP BY; it only needs at least one qualifying row.
Common Pitfalls:
Selecting many columns inside the EXISTS subquery (unnecessary), or expecting EXISTS to return data rather than a Boolean.
Final Answer:
Correct
Discussion & Comments