0
0
PostgreSQLquery~20 mins

Why set operations matter in PostgreSQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Set Operations Mastery
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: 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?
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;
AAlice, Charlie, Diana, Edward
BAlice, Bob, Charlie, Diana, Edward
CBob, Diana, Edward
DAlice, Bob, Charlie, Bob, Diana, Edward
Attempts:
2 left
💡 Hint
UNION removes duplicates and combines all unique rows from both queries.
query_result
intermediate
2: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?
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;
AAlice, Bob, Charlie, Diana, Edward
BBob
CDiana, Edward
DAlice, Charlie
Attempts:
2 left
💡 Hint
EXCEPT returns rows in the first query that are not in the second.
📝 Syntax
advanced
2: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?
ASELECT name FROM students_a INTERSECT SELECT name FROM students_b;
BSELECT name FROM students_a UNION INTERSECT SELECT name FROM students_b;
CSELECT name FROM students_a INTERSECT ALL SELECT name FROM students_b;
DSELECT name FROM students_a EXCEPT INTERSECT SELECT name FROM students_b;
Attempts:
2 left
💡 Hint
INTERSECT combines two queries and returns only rows present in both.
optimization
advanced
2: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?
AUse INTERSECT to find unique names.
BUse UNION ALL followed by SELECT DISTINCT on the combined result.
CUse UNION which removes duplicates automatically.
DUse EXCEPT to remove duplicates.
Attempts:
2 left
💡 Hint
UNION removes duplicates during the operation, avoiding extra steps.
🧠 Conceptual
expert
3: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?
ABecause set operations combine rows vertically, each column must match in position and type to merge correctly.
BBecause set operations join tables horizontally, columns can differ in number and type.
CBecause PostgreSQL automatically converts all data types to text during set operations.
DBecause set operations only work on numeric columns.
Attempts:
2 left
💡 Hint
Think about how rows from different queries stack on top of each other.