Challenge - 5 Problems
UNION Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Difference in row count between UNION and UNION ALL
Consider two tables,
What is the number of rows returned by the following queries?
and
Assume
Employees and Contractors, each with a column name. What is the number of rows returned by the following queries?
SELECT name FROM Employees UNION SELECT name FROM Contractors;and
SELECT name FROM Employees UNION ALL SELECT name FROM Contractors;Assume
Employees has 3 rows: Alice, Bob, Charlie and Contractors has 3 rows: Bob, Dave, Eve.Attempts:
2 left
💡 Hint
UNION removes duplicates, UNION ALL does not.
✗ Incorrect
UNION combines rows and removes duplicates, so Bob appears once. UNION ALL combines all rows including duplicates.
🧠 Conceptual
intermediate2:00remaining
Behavior of UNION with different column names
Given two tables:
Which statement about the query below is true?
Table1(id INT, name VARCHAR(10))Table2(user_id INT, username VARCHAR(10))Which statement about the query below is true?
SELECT id, name FROM Table1 UNION SELECT user_id, username FROM Table2;Attempts:
2 left
💡 Hint
UNION matches columns by position, not by name.
✗ Incorrect
UNION combines results by matching columns in order, not by their names. Different column names do not cause errors.
📝 Syntax
advanced2:00remaining
Identify the syntax error in UNION query
Which of the following UNION queries will cause a syntax error in MySQL?
Attempts:
2 left
💡 Hint
ORDER BY must be at the end of the entire UNION query.
✗ Incorrect
ORDER BY inside the first SELECT before UNION is invalid syntax. ORDER BY applies to the final combined result.
❓ optimization
advanced2:00remaining
Performance impact of UNION vs UNION ALL
You have two large tables with millions of rows each. You want to combine their rows including duplicates. Which query is more efficient and why?
Attempts:
2 left
💡 Hint
Removing duplicates requires extra work.
✗ Incorrect
UNION removes duplicates which requires sorting or hashing, making it slower. UNION ALL just concatenates rows, so it is faster.
🔧 Debug
expert2:00remaining
Why does this UNION query fail?
Given the query:
Why does this query fail?
SELECT id, name FROM Employees UNION SELECT id FROM Contractors;Why does this query fail?
Attempts:
2 left
💡 Hint
UNION requires the same number of columns in each SELECT.
✗ Incorrect
UNION requires each SELECT to have the same number of columns with compatible types. Here, the second SELECT has only one column.