Challenge - 5 Problems
Set Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of UNION with ORDER BY on combined result
Consider two tables EmployeesA and EmployeesB with a single column
name. What is the output of the following query?SELECT name FROM EmployeesA UNION SELECT name FROM EmployeesB ORDER BY name ASC;
SQL
CREATE TABLE EmployeesA(name VARCHAR(20)); INSERT INTO EmployeesA VALUES ('Alice'), ('Bob'), ('Charlie'); CREATE TABLE EmployeesB(name VARCHAR(20)); INSERT INTO EmployeesB VALUES ('Bob'), ('Diana'), ('Eve');
Attempts:
2 left
💡 Hint
UNION removes duplicates and ORDER BY sorts the combined result.
✗ Incorrect
UNION combines rows from both tables and removes duplicates. Then ORDER BY sorts the unique names alphabetically.
❓ query_result
intermediate2:00remaining
Effect of ORDER BY inside individual SELECTs with UNION ALL
Given two tables
Numbers1 and Numbers2 each with a column num, what is the output of this query?SELECT num FROM Numbers1 ORDER BY num DESC UNION ALL SELECT num FROM Numbers2 ORDER BY num ASC;
SQL
CREATE TABLE Numbers1(num INT); INSERT INTO Numbers1 VALUES (3), (1), (2); CREATE TABLE Numbers2(num INT); INSERT INTO Numbers2 VALUES (4), (5);
Attempts:
2 left
💡 Hint
ORDER BY is not allowed inside individual SELECTs in UNION unless wrapped in subqueries.
✗ Incorrect
ORDER BY clauses inside individual SELECT statements in a UNION cause syntax errors in standard SQL. ORDER BY should be applied once after the UNION.
📝 Syntax
advanced2:00remaining
Correct placement of ORDER BY with INTERSECT
Which of the following queries correctly returns the common
id values from tables Table1 and Table2 sorted in descending order?Attempts:
2 left
💡 Hint
ORDER BY applies to the final result of the set operation.
✗ Incorrect
ORDER BY must come after the entire INTERSECT operation to sort the final result. Option D places ORDER BY correctly.
❓ query_result
advanced2:00remaining
Output of EXCEPT with ORDER BY and duplicates
Given tables
SetA and SetB with column val, what is the output of this query?SELECT val FROM SetA EXCEPT SELECT val FROM SetB ORDER BY val ASC;
SQL
CREATE TABLE SetA(val INT); INSERT INTO SetA VALUES (1), (2), (2), (3); CREATE TABLE SetB(val INT); INSERT INTO SetB VALUES (2);
Attempts:
2 left
💡 Hint
EXCEPT removes all values from SetA that appear in SetB, duplicates removed.
✗ Incorrect
EXCEPT returns distinct values in SetA not in SetB. Since 2 is in SetB, all 2s are excluded. Result sorted ascending is 1 and 3.
🧠 Conceptual
expert2:00remaining
Why ORDER BY must be last in set operations
Why is it required that the
ORDER BY clause appears only once at the end of a query combining multiple SELECTs with set operations like UNION, INTERSECT, or EXCEPT?Attempts:
2 left
💡 Hint
Think about how set operations treat their inputs and outputs.
✗ Incorrect
Set operations treat their inputs as unordered sets. Sorting before combining would be meaningless. ORDER BY must be applied once after the full combined result is ready.