Challenge - 5 Problems
UNION Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this UNION query?
Consider two tables:
Table A:
id | name
1 | Alice
2 | Bob
Table B:
id | name
2 | Bob
3 | Carol
What rows will this query return?
Table A:
id | name
1 | Alice
2 | Bob
Table B:
id | name
2 | Bob
3 | Carol
What rows will this query return?
SELECT id, name FROM A
UNION
SELECT id, name FROM B;
Attempts:
2 left
💡 Hint
UNION removes duplicate rows from the combined result.
✗ Incorrect
UNION combines rows from both tables but removes duplicates. Since '2 | Bob' appears in both, it shows only once.
📝 Syntax
intermediate2:00remaining
Which UNION query is syntactically correct?
Choose the correct SQL UNION syntax from the options below:
Attempts:
2 left
💡 Hint
UNION combines result sets; ORDER BY applies to the whole UNION result and must be placed at the end.
✗ Incorrect
Option D is the basic correct syntax for UNION. Options C and D have ORDER BY after the second SELECT without parentheses, which is invalid. Option D uses UNION ALL, which is valid syntax but different from UNION.
❓ optimization
advanced2:00remaining
Which UNION query is more efficient for combining two large tables without duplicates?
Given two large tables A and B with the same columns, which query is best to combine all unique rows?
Attempts:
2 left
💡 Hint
UNION removes duplicates automatically, UNION ALL does not.
✗ Incorrect
Option A uses UNION which removes duplicates efficiently. Option A returns duplicates. Option A does UNION ALL then DISTINCT which is less efficient. Option A returns only rows common to both tables.
🔧 Debug
advanced2:00remaining
Why does this UNION query cause an error?
Given tables A(id INT, name VARCHAR) and B(id INT, age INT), why does this query fail?
SELECT id, name FROM A UNION SELECT id, age FROM B;
Attempts:
2 left
💡 Hint
UNION requires matching column types in order.
✗ Incorrect
The first SELECT returns (id INT, name VARCHAR), the second returns (id INT, age INT). The second column types differ (VARCHAR vs INT), causing a type mismatch error.
🧠 Conceptual
expert2:00remaining
What is the difference between UNION and UNION ALL?
Choose the statement that correctly explains the difference between UNION and UNION ALL:
Attempts:
2 left
💡 Hint
Think about how duplicates are handled in each.
✗ Incorrect
UNION removes duplicates, so the result has unique rows. UNION ALL returns all rows from both queries, including duplicates. UNION ALL is usually faster because it does not check for duplicates.