In standard SQL querying, the EXISTS predicate evaluates to true if the subquery returns at least one row that satisfies the given condition. Is that interpretation correct?

Database Database Redesign Difficulty: Easy
Choose an option
  • A
    Correct
  • B
    Incorrect
  • C
    Only true if the subquery returns exactly one row
  • D
    Only true when used with GROUP BY

Answer

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:

  • The subquery can be correlated (referencing outer query columns) or uncorrelated.
  • EXISTS returns a Boolean: true if at least one row qualifies, false otherwise.
  • The subquery typically selects a minimal set of columns (often SELECT 1).

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
No comments yet. Be the first to comment!
Join Discussion