0
0
SQLquery~20 mins

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

Choose your learning style9 modes available
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.