In SQL, which description best defines a UNION query?

Difficulty: Easy

Correct Answer: It combines the output from multiple queries and requires the same number of columns in each query.

Explanation:


Introduction / Context:
UNION queries are used in SQL to combine the results of two or more SELECT statements into a single result set. They are common in reporting and data integration scenarios where similar data is stored in multiple tables. Interviewers ask this kind of question to verify that you know the structural requirements of UNION, especially the rule about matching numbers of columns and compatible data types across the combined queries.


Given Data / Assumptions:

    - The term UNION refers to the SQL UNION operator, not UNION ALL.
    - Each SELECT in the UNION must produce rows with the same number of columns.
    - The corresponding columns in each SELECT should have compatible data types.
    - The result set of the UNION is the vertical concatenation of these SELECT outputs with duplicate rows removed by default.


Concept / Approach:
The UNION operator combines results from multiple SELECT statements by stacking rows on top of each other. For this to work, each SELECT must return the same number of columns, and each corresponding column must have compatible types so that values can be merged into a single logical column. Most implementations also require that column order and type compatibility match. By default, UNION removes duplicate rows, while UNION ALL keeps all rows including duplicates. The key structural rule asked about in this question is the requirement for the same number of columns in each component query.


Step-by-Step Solution:
Step 1: Recall that UNION allows combining more than two SELECT statements, not just exactly two. Step 2: Remember that each SELECT in a UNION must output the same number of columns. Step 3: The data types of corresponding columns should be compatible so that they can form a single result column. Step 4: Identify the option that says it combines outputs from multiple queries and requires the same number of columns. Step 5: Select option B as it correctly states both the ability to combine multiple queries and the same column count requirement.


Verification / Alternative check:
Imagine writing a UNION query such as SELECT id, name FROM TableA UNION SELECT id, name FROM TableB UNION SELECT id, name FROM TableC. All three SELECT statements return two columns, id and name. If you tried to UNION a SELECT with two columns and another with three columns, most SQL engines would raise an error because the column counts do not match. This directly supports the rule described in option B and contradicts the statements in the other options.


Why Other Options Are Wrong:
Option A is incorrect because it states that the same number of columns is not required, which is contrary to SQL rules. Option C is wrong for two reasons: it limits UNION to no more than two queries and again claims that column counts can differ. Option D is partially correct about requiring the same number of columns but still incorrectly limits UNION to no more than two queries. In practice, many queries can be combined using UNION by chaining them.


Common Pitfalls:
A common pitfall is to confuse UNION with JOIN. UNION combines rows from similar result sets, while JOIN combines columns from related tables based on key relationships. Another mistake is to forget about type compatibility, which can lead to implicit conversions or runtime errors. In interviews, emphasize that UNION combines multiple SELECT results with matching column structures and that UNION ALL is used when you want to keep duplicates instead of eliminating them.


Final Answer:
A UNION query combines the output from multiple SELECT statements and requires each query to return the same number of columns with compatible types.

Discussion & Comments

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