Difficulty: Easy
Correct Answer: Combines the output from multiple queries and must include the same number of columns.
Explanation:
Introduction / Context:
The SQL UNION operator merges the results of two or more SELECT queries into a single result set. Mastering UNION (and UNION ALL) is crucial for reporting, data consolidation across similar tables, and combining filtered slices of the same table. This question checks your understanding of the structural requirements for valid UNION operations.
Given Data / Assumptions:
Concept / Approach:
UNION vertically appends rows from each SELECT. Because the final result set is one table-shaped structure, all SELECTs must share the same column count and compatible data types in corresponding positions. UNION removes duplicates; UNION ALL preserves them.
Step-by-Step Solution:
Verification / Alternative check:
Create a quick example: SELECT id FROM A UNION SELECT id FROM B UNION SELECT id FROM C is valid, demonstrating more than two queries; each returns one column.
Why Other Options Are Wrong:
Two queries only is incorrect; SQL supports multiple SELECTs.
Different column counts is invalid; columns must align in count (and be type-compatible).
Common Pitfalls:
Confusing UNION with JOIN. JOIN combines columns horizontally based on matching rows, whereas UNION stacks rows vertically from separate SELECTs.
Final Answer:
Combines the output from multiple queries and must include the same number of columns.
Discussion & Comments