Difficulty: Medium
Correct Answer: A subquery has a distinct form that cannot be duplicated by a join in all cases.
Explanation:
Introduction:
SQL supports both joins and subqueries for composing results. While many subqueries can be rewritten as joins, there are edge cases and semantic differences that make direct transformation non-trivial or impossible without additional constructs. This question evaluates your understanding of those nuances.
Given Data / Assumptions:
Concept / Approach:
Many uncorrelated subqueries can be rewritten as joins. However, correlated subqueries with EXISTS/NOT EXISTS or conditions that are row-wise or existential in nature may not map cleanly to a simple join without changing semantics, especially when duplicates, NULL handling, or set containment are involved. Moreover, aggregate subqueries per row may require lateral joins or window functions, not plain joins.
Step-by-Step Solution:
1) Recognize that option (b) overstates equivalence; there are cases where joins do not capture the same semantics succinctly.2) Note that option (a) is false; subqueries are not limited to two tables.3) Option (d) is misleading; ORDER BY inside subqueries is typically ignored unless used with TOP/OFFSET or in derived tables.4) Option (e) is false; subqueries are allowed in WHERE/HAVING and more.5) Select (c) to acknowledge that subqueries can express logic not trivially replicated by joins.
Verification / Alternative check:
EXISTS and NOT EXISTS semantics, anti-joins, and per-row aggregates often require specialized constructs beyond basic inner/outer joins to match exactly, especially in the presence of NULLs and duplicates.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming performance or equivalence without considering duplicates, NULL semantics, or correlation. Use the construct that expresses intent clearly and efficiently in your DBMS.
Final Answer:
A subquery has a distinct form that cannot be duplicated by a join in all cases.
Discussion & Comments