Challenge - 5 Problems
Set Operation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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?
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;
Attempts:
2 left
💡 Hint
UNION matches columns by position, not by name.
✗ Incorrect
In set operations like UNION, columns are matched by their position, not by their names. So the first column of A matches the first column of B, and the second column matches the second. UNION removes duplicates, so Bob appears only once.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how rows are combined in set operations.
✗ Incorrect
Set operations require both SELECT statements to have the same number of columns because rows are combined positionally.
📝 Syntax
advanced1: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;
Attempts:
2 left
💡 Hint
Check the number of columns in each SELECT.
✗ Incorrect
The first SELECT returns two columns, the second only one. Set operations require the same number of columns.
❓ optimization
advanced2: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:
vs
Options:
SELECT * FROM A UNION ALL SELECT * FROM B;
vs
SELECT * FROM A UNION SELECT * FROM B;
Attempts:
2 left
💡 Hint
Think about what extra work UNION does compared to UNION ALL.
✗ Incorrect
UNION removes duplicates which requires sorting or hashing, adding overhead. UNION ALL simply concatenates results without extra processing.
🔧 Debug
expert2: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?
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;
Attempts:
2 left
💡 Hint
Check how INTERSECT compares values with different data types.
✗ Incorrect
INTERSECT compares values including data types. Since INT and VARCHAR are different, no matches occur without explicit casting.