In SQL (ANSI/ISO SQL-92 and later), what does a UNION query do? Choose the most accurate description of how UNION combines result sets.

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:

  • You can place more than two SELECT statements in one UNION chain (for example, SELECT ... UNION SELECT ... UNION SELECT ...).
  • Each SELECT in the UNION must project the same number of columns.
  • Corresponding columns should be type-compatible so they can be aligned into a single set of columns.


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:

Identify the structural rule: each SELECT must return the same number of columns.Confirm that SQL allows more than two SELECTs in a UNION chain.Select the choice that states “multiple queries” and “same number of columns.”


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

No comments yet. Be the first to comment!
Join Discussion