Challenge - 5 Problems
Set Operations Mastery
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: students_a and students_b, each with a single column name.
students_a has names: Alice, Bob, Charlie.
students_b has names: Bob, Diana, Edward.
What is the result of this query?
students_a has names: Alice, Bob, Charlie.
students_b has names: Bob, Diana, Edward.
What is the result of this query?
SELECT name FROM students_a
UNION
SELECT name FROM students_b;
PostgreSQL
CREATE TABLE students_a(name TEXT); INSERT INTO students_a VALUES ('Alice'), ('Bob'), ('Charlie'); CREATE TABLE students_b(name TEXT); INSERT INTO students_b VALUES ('Bob'), ('Diana'), ('Edward'); SELECT name FROM students_a UNION SELECT name FROM students_b;
Attempts:
2 left
💡 Hint
UNION removes duplicates and combines all unique rows from both queries.
✗ Incorrect
The UNION operation combines rows from both tables but removes duplicates. Bob appears in both tables but only once in the result.
❓ query_result
intermediate2:00remaining
What does EXCEPT return in this query?
Given the same tables students_a and students_b as before,
what is the output of this query?
what is the output of this query?
SELECT name FROM students_a
EXCEPT
SELECT name FROM students_b;
PostgreSQL
CREATE TABLE students_a(name TEXT); INSERT INTO students_a VALUES ('Alice'), ('Bob'), ('Charlie'); CREATE TABLE students_b(name TEXT); INSERT INTO students_b VALUES ('Bob'), ('Diana'), ('Edward'); SELECT name FROM students_a EXCEPT SELECT name FROM students_b;
Attempts:
2 left
💡 Hint
EXCEPT returns rows in the first query that are not in the second.
✗ Incorrect
Bob is in both tables, so it is excluded. Alice and Charlie are only in students_a, so they appear.
📝 Syntax
advanced2:00remaining
Which query correctly uses INTERSECT to find common names?
You want to find names that appear in both students_a and students_b. Which query is correct?
Attempts:
2 left
💡 Hint
INTERSECT combines two queries and returns only rows present in both.
✗ Incorrect
Option A is the correct syntax for INTERSECT. Options B and D have invalid syntax. Option A uses INTERSECT ALL, which is supported in PostgreSQL but preserves duplicate rows if they exist in both result sets.
❓ optimization
advanced2:00remaining
Which approach is more efficient to find unique names from two large tables?
You have two large tables with millions of rows each. You want to get all unique names from both tables.
Which query is generally more efficient in PostgreSQL?
Which query is generally more efficient in PostgreSQL?
Attempts:
2 left
💡 Hint
UNION removes duplicates during the operation, avoiding extra steps.
✗ Incorrect
UNION is optimized to remove duplicates during the merge. UNION ALL with DISTINCT requires an extra step and can be slower. INTERSECT and EXCEPT do not return all unique names.
🧠 Conceptual
expert3:00remaining
Why do set operations require queries to have the same number of columns and compatible types?
Set operations like UNION, INTERSECT, and EXCEPT combine results from multiple queries.
Why must these queries have the same number of columns and compatible data types?
Why must these queries have the same number of columns and compatible data types?
Attempts:
2 left
💡 Hint
Think about how rows from different queries stack on top of each other.
✗ Incorrect
Set operations stack rows from queries. Each column position must match in number and type to combine rows properly. Different numbers or incompatible types cause errors.