Challenge - 5 Problems
INTERSECT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find common employees in two departments
Given two tables Sales and Marketing with a column
employee_id, which query returns the employee IDs that work in both departments?SQL
SELECT employee_id FROM Sales INTERSECT SELECT employee_id FROM Marketing;
Attempts:
2 left
💡 Hint
INTERSECT returns only rows present in both queries.
✗ Incorrect
The INTERSECT operator returns only the employee IDs that appear in both Sales and Marketing tables. So only employees 101 and 103 are common.
📝 Syntax
intermediate1:30remaining
Identify the correct INTERSECT syntax
Which of the following SQL queries correctly uses INTERSECT to find common product IDs between two tables
InventoryA and InventoryB?Attempts:
2 left
💡 Hint
INTERSECT does not combine with UNION or use ALL or BY keywords.
✗ Incorrect
Option A uses the correct syntax: SELECT ... INTERSECT SELECT ... without extra keywords. Others are invalid syntax.
❓ optimization
advanced2:30remaining
Optimize query to find common customers
You want to find customers who bought products in both
Orders2023 and Orders2024. Which query is the most efficient to get common customer IDs?Attempts:
2 left
💡 Hint
INTERSECT is designed to efficiently find common rows between two queries.
✗ Incorrect
Option B uses INTERSECT which is optimized for this purpose. Option B uses subquery which can be slower. Option B returns duplicates if multiple matches. Option B returns all unique customers, not just common ones.
🧠 Conceptual
advanced2:00remaining
Understanding INTERSECT with duplicates
Consider two tables with these rows:
Table A: (1), (1), (2), (3)
Table B: (1), (2), (2), (4)
What will the result of
Table A: (1), (1), (2), (3)
Table B: (1), (2), (2), (4)
What will the result of
SELECT val FROM A INTERSECT SELECT val FROM B; be?Attempts:
2 left
💡 Hint
INTERSECT removes duplicates and returns only common distinct values.
✗ Incorrect
INTERSECT returns distinct values present in both tables. So only 1 and 2 appear in both, once each.
🔧 Debug
expert3:00remaining
Why does this INTERSECT query fail?
Given two tables
Employees and Managers with different column counts, why does this query cause an error?SELECT id, name FROM Employees INTERSECT SELECT id FROM Managers;SQL
SELECT id, name FROM Employees INTERSECT SELECT id FROM Managers;
Attempts:
2 left
💡 Hint
INTERSECT requires matching column counts and compatible types.
✗ Incorrect
INTERSECT requires both SELECT statements to have the same number of columns with compatible data types. Here, one has two columns, the other one, causing an error.