0
0
SQLquery~20 mins

UNION combining result sets in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UNION Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?
SELECT id, name FROM A
UNION
SELECT id, name FROM B;
A2 | Bob<br>3 | Carol
B1 | Alice<br>2 | Bob<br>2 | Bob<br>3 | Carol
C1 | Alice<br>2 | Bob<br>3 | Carol
D1 | Alice<br>3 | Carol
Attempts:
2 left
💡 Hint
UNION removes duplicate rows from the combined result.
📝 Syntax
intermediate
2:00remaining
Which UNION query is syntactically correct?
Choose the correct SQL UNION syntax from the options below:
ASELECT id, name FROM A UNION SELECT id, name FROM B ORDER BY name LIMIT 5;
BSELECT id, name FROM A UNION ALL SELECT id, name FROM B;
CSELECT id, name FROM A UNION SELECT id, name FROM B ORDER BY name;
DSELECT id, name FROM A UNION SELECT id, name FROM B;
Attempts:
2 left
💡 Hint
UNION combines result sets; ORDER BY applies to the whole UNION result and must be placed at the end.
optimization
advanced
2: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?
ASELECT * FROM A UNION SELECT * FROM B;
BSELECT * FROM A UNION ALL SELECT * FROM B;
CSELECT DISTINCT * FROM (SELECT * FROM A UNION ALL SELECT * FROM B) AS combined;
DSELECT * FROM A INTERSECT SELECT * FROM B;
Attempts:
2 left
💡 Hint
UNION removes duplicates automatically, UNION ALL does not.
🔧 Debug
advanced
2: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;
ABecause the column data types in the same position differ.
BBecause table B does not have a 'name' column.
CBecause UNION requires ORDER BY clause.
DBecause the number of columns in both SELECTs differ.
Attempts:
2 left
💡 Hint
UNION requires matching column types in order.
🧠 Conceptual
expert
2:00remaining
What is the difference between UNION and UNION ALL?
Choose the statement that correctly explains the difference between UNION and UNION ALL:
AUNION and UNION ALL behave the same but UNION ALL requires ORDER BY.
BUNION removes duplicate rows from the combined result; UNION ALL includes all rows, duplicates included.
CUNION is faster than UNION ALL because it skips duplicates.
DUNION includes all rows, duplicates included; UNION ALL removes duplicates.
Attempts:
2 left
💡 Hint
Think about how duplicates are handled in each.