Bird
Raised Fist0
SQLquery~20 mins

Set operation column matching rules in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Set Operation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of UNION with different column names
Consider two tables:

Table A:
id | name
1 | Alice
2 | Bob

Table B:
user_id | username
2 | Bob
3 | Charlie

What is the output of the following SQL query?
SELECT id, name FROM A
UNION
SELECT user_id, username FROM B;
SQL
CREATE TABLE A (id INT, name VARCHAR(10));
INSERT INTO A VALUES (1, 'Alice'), (2, 'Bob');
CREATE TABLE B (user_id INT, username VARCHAR(10));
INSERT INTO B VALUES (2, 'Bob'), (3, 'Charlie');

SELECT id, name FROM A
UNION
SELECT user_id, username FROM B;
A
1 | Alice
3 | Charlie
B
1 | Alice
2 | Bob
3 | Charlie
CSyntax error due to different column names
D
1 | Alice
2 | Bob
2 | Bob
3 | Charlie
Attempts:
2 left
💡 Hint
UNION matches columns by position, not by name.
🧠 Conceptual
intermediate
1:30remaining
Column count requirement in set operations
Which of the following statements about set operations (UNION, INTERSECT, EXCEPT) is true regarding the number of columns in the SELECT statements?
ABoth SELECT statements must have the same number of columns.
BThe first SELECT can have more columns than the second.
CThe second SELECT can have more columns than the first.
DThe number of columns does not matter as long as the data types match.
Attempts:
2 left
💡 Hint
Think about how rows are combined in set operations.
📝 Syntax
advanced
1:30remaining
Error caused by mismatched column counts
What error will this SQL query produce?
SELECT id, name FROM A
UNION
SELECT user_id FROM B;
AError: Data types of columns do not match
BNo error, query runs and returns combined rows
CError: Column names must match exactly
DError: Each SELECT statement must have the same number of columns
Attempts:
2 left
💡 Hint
Check the number of columns in each SELECT.
optimization
advanced
2:00remaining
Improving performance of UNION ALL vs UNION
Given two large tables with identical column structures, which set operation is generally faster and why?

Options:
SELECT * FROM A
UNION ALL
SELECT * FROM B;

vs

SELECT * FROM A
UNION
SELECT * FROM B;
AUNION is faster because it removes duplicates efficiently.
BBoth have the same performance because they scan the same data.
CUNION ALL is faster because it does not remove duplicates.
DUNION ALL is slower because it returns all rows including duplicates.
Attempts:
2 left
💡 Hint
Think about what extra work UNION does compared to UNION ALL.
🔧 Debug
expert
2:30remaining
Diagnosing unexpected output from INTERSECT with mismatched data types
Consider these tables:

Table X:
id INT
1
2
3

Table Y:
id VARCHAR(10)
'2'
'3'
'4'

What will be the result of this query?
SELECT id FROM X
INTERSECT
SELECT id FROM Y;
AEmpty result set because data types differ and no implicit conversion occurs.
BSyntax error due to different data types in INTERSECT.
CRows with id 2 and 3 because values match despite different types.
DRows with id 1, 2, and 3 because INTERSECT ignores data types.
Attempts:
2 left
💡 Hint
Check how INTERSECT compares values with different data types.

Practice

(1/5)
1.

Which rule must be followed when using SQL set operations like UNION or INTERSECT?

easy
A. Both queries must have the same number of columns with compatible data types.
B. The second query must have more columns than the first.
C. Column names must be identical in both queries.
D. Set operations only work with numeric columns.

Solution

  1. Step 1: Understand set operation requirements

    Set operations combine results from multiple queries, so columns must match in number and type to align data correctly.
  2. Step 2: Check column name and type rules

    Column names do not need to match; only the first query's column names are used. Data types must be compatible, and the number of columns must be the same.
  3. Final Answer:

    Both queries must have the same number of columns with compatible data types. -> Option A
  4. Quick Check:

    Column count and type compatibility = A [OK]
Hint: Match column count and types for set operations [OK]
Common Mistakes:
  • Assuming column names must match
  • Using different number of columns
  • Trying set operations on incompatible data types
2.

Which of the following SQL queries correctly uses UNION with matching columns?

-- Table A: (id INT, name VARCHAR)
-- Table B: (user_id INT, username VARCHAR)

A) SELECT id, name FROM A UNION SELECT user_id, username FROM B;
B) SELECT id FROM A UNION SELECT user_id, username FROM B;
C) SELECT id, name FROM A UNION SELECT user_id FROM B;
D) SELECT id, name FROM A UNION SELECT user_id, username, email FROM B;
easy
A. SELECT id, name FROM A UNION SELECT user_id FROM B;
B. SELECT id FROM A UNION SELECT user_id, username FROM B;
C. SELECT id, name FROM A UNION SELECT user_id, username FROM B;
D. SELECT id, name FROM A UNION SELECT user_id, username, email FROM B;

Solution

  1. Step 1: Check column counts in each query

    SELECT id, name FROM A UNION SELECT user_id, username FROM B; selects 2 columns from both queries, matching counts. Other options have mismatched column counts (1 vs 2, 2 vs 1, or 2 vs 3).
  2. Step 2: Verify column types compatibility

    Columns in SELECT id, name FROM A UNION SELECT user_id, username FROM B; are INT and VARCHAR in both queries, which are compatible.
  3. Final Answer:

    SELECT id, name FROM A UNION SELECT user_id, username FROM B; -> Option C
  4. Quick Check:

    Equal columns and compatible types = A [OK]
Hint: Count columns and check types match in both queries [OK]
Common Mistakes:
  • Mismatched column counts cause errors
  • Ignoring column type compatibility
  • Assuming extra columns are allowed
3.

Given the tables and query below, what will be the output?

Table X:
id | value
1 | 'A'
2 | 'B'

Table Y:
code | val
2 | 'B'
3 | 'C'

Query:
SELECT id, value FROM X
UNION
SELECT code, val FROM Y;
medium
A. Syntax error due to column name mismatch
B. [ (1, 'A'), (2, 'B'), (3, 'C') ]
C. [ (1, 'A'), (3, 'C') ]
D. [ (1, 'A'), (2, 'B'), (2, 'B'), (3, 'C') ]

Solution

  1. Step 1: Check column counts and types

    Both queries select 2 columns with compatible types (INT and VARCHAR). Column names differ but that is allowed.
  2. Step 2: Understand UNION behavior

    UNION removes duplicates. Rows (2, 'B') appear in both tables, so only one copy appears in result.
  3. Final Answer:

    [ (1, 'A'), (2, 'B'), (3, 'C') ] -> Option B
  4. Quick Check:

    UNION removes duplicates = D [OK]
Hint: UNION removes duplicates, column names don't matter [OK]
Common Mistakes:
  • Expecting duplicate rows in UNION result
  • Thinking column names must match
  • Confusing UNION with UNION ALL
4.

Identify the error in the following SQL set operation:

SELECT id, name FROM Customers
UNION
SELECT customer_id FROM Orders;
medium
A. Column names do not match between queries.
B. UNION cannot be used with SELECT statements.
C. Data types of columns are incompatible.
D. The second query has fewer columns than the first.

Solution

  1. Step 1: Compare column counts in both queries

    The first query selects 2 columns (id, name), the second selects only 1 column (customer_id). This mismatch causes an error.
  2. Step 2: Check other possible errors

    Column names do not need to match, and UNION works with SELECT statements. Data types are unknown but column count mismatch is enough to cause error.
  3. Final Answer:

    The second query has fewer columns than the first. -> Option D
  4. Quick Check:

    Column count mismatch = B [OK]
Hint: Ensure both queries select same number of columns [OK]
Common Mistakes:
  • Thinking column names must match
  • Ignoring column count mismatch
  • Assuming UNION works with different column counts
5.

You have two tables:

Employees(emp_id INT, emp_name VARCHAR, dept VARCHAR)
Managers(manager_id INT, manager_name VARCHAR)

You want to combine employee and manager names into one list using a set operation. Which query correctly applies set operation column matching rules?

A) SELECT emp_id, emp_name FROM Employees UNION SELECT manager_id, manager_name FROM Managers;
B) SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers;
C) SELECT emp_name, dept FROM Employees UNION SELECT manager_name, manager_id FROM Managers;
D) SELECT emp_id, emp_name, dept FROM Employees UNION SELECT manager_id, manager_name FROM Managers;
hard
A. SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers;
B. SELECT emp_id, emp_name FROM Employees UNION SELECT manager_id, manager_name FROM Managers;
C. SELECT emp_name, dept FROM Employees UNION SELECT manager_name, manager_id FROM Managers;
D. SELECT emp_id, emp_name, dept FROM Employees UNION SELECT manager_id, manager_name FROM Managers;

Solution

  1. Step 1: Identify columns to combine

    You want a list of names only, so selecting one column (name) from each table is appropriate.
  2. Step 2: Check column counts and types

    SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers; selects one VARCHAR column from each table, matching count and compatible types. Other options have mismatched column counts or incompatible columns.
  3. Final Answer:

    SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers; -> Option A
  4. Quick Check:

    Same column count and type for names = B [OK]
Hint: Select same number and type of columns to combine [OK]
Common Mistakes:
  • Selecting different numbers of columns
  • Mixing incompatible column types
  • Including unrelated columns in set operation