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 INTERSECT query?
Given two tables Employees_A and Employees_B with the following data:
Employees_A
id | name
1 | Alice
2 | Bob
3 | Carol
Employees_B
id | name
2 | Bob
3 | Carol
4 | Dave
What will be the result of this query?
Employees_A
id | name
1 | Alice
2 | Bob
3 | Carol
Employees_B
id | name
2 | Bob
3 | Carol
4 | Dave
What will be the result of this query?
SELECT id, name FROM Employees_A
INTERSECT
SELECT id, name FROM Employees_B;
Attempts:
2 left
💡 Hint
INTERSECT returns only rows that appear in both queries.
✗ Incorrect
The INTERSECT operator returns only the rows that exist in both Employees_A and Employees_B. Here, Bob and Carol are common in both tables.
❓ query_result
intermediate2:00remaining
What rows does this EXCEPT query return?
Given two tables Products_2023 and Products_2024 with data:
Products_2023
id | product
1 | Chair
2 | Table
3 | Lamp
Products_2024
id | product
2 | Table
3 | Lamp
4 | Sofa
What is the output of this query?
Products_2023
id | product
1 | Chair
2 | Table
3 | Lamp
Products_2024
id | product
2 | Table
3 | Lamp
4 | Sofa
What is the output of this query?
SELECT id, product FROM Products_2023
EXCEPT
SELECT id, product FROM Products_2024;
Attempts:
2 left
💡 Hint
EXCEPT returns rows in the first query that are not in the second.
✗ Incorrect
The EXCEPT operator returns rows from Products_2023 that do not appear in Products_2024. Only 'Chair' is unique to Products_2023.
🧠 Conceptual
advanced2:00remaining
Which statement about INTERSECT and EXCEPT is true?
Consider the following statements about INTERSECT and EXCEPT in SQL:
A) INTERSECT returns all rows from the first query plus rows from the second.
B) EXCEPT returns rows that appear in both queries.
C) INTERSECT returns only rows common to both queries.
D) EXCEPT returns all rows from the second query that are not in the first.
Which statement is correct?
A) INTERSECT returns all rows from the first query plus rows from the second.
B) EXCEPT returns rows that appear in both queries.
C) INTERSECT returns only rows common to both queries.
D) EXCEPT returns all rows from the second query that are not in the first.
Which statement is correct?
Attempts:
2 left
💡 Hint
Think about what INTERSECT and EXCEPT do with sets of rows.
✗ Incorrect
INTERSECT returns only the rows that exist in both queries. EXCEPT returns rows from the first query that do not appear in the second.
📝 Syntax
advanced2:00remaining
Which query will cause a syntax error?
Which of the following SQL queries using INTERSECT or EXCEPT will cause a syntax error in PostgreSQL?
Attempts:
2 left
💡 Hint
Check if the number and order of columns match in both queries.
✗ Incorrect
Option A selects two columns in the first query but only one column in the second, causing a syntax error because INTERSECT and EXCEPT require the same number of columns with compatible types.
❓ optimization
expert3:00remaining
How to optimize this EXCEPT query for large tables?
You have two large tables, Orders_2023 and Orders_2024, both with millions of rows and the same schema.
You want to find orders in 2023 that are not in 2024 using:
Which optimization will most improve performance?
You want to find orders in 2023 that are not in 2024 using:
SELECT * FROM Orders_2023 EXCEPT SELECT * FROM Orders_2024;
Which optimization will most improve performance?
Attempts:
2 left
💡 Hint
Think about how EXCEPT works internally with sorting and memory.
✗ Incorrect
EXCEPT requires sorting or hashing to remove duplicates. Increasing work_mem allows PostgreSQL to perform these operations in memory, reducing disk I/O and improving performance.