0
0
SQLquery~20 mins

INTERSECT for common rows in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
INTERSECT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"employee_id": 102}, {"employee_id": 104}]
B[]
C[{"employee_id": 101}, {"employee_id": 102}, {"employee_id": 103}]
D[{"employee_id": 101}, {"employee_id": 103}]
Attempts:
2 left
💡 Hint
INTERSECT returns only rows present in both queries.
📝 Syntax
intermediate
1: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?
ASELECT product_id FROM InventoryA INTERSECT SELECT product_id FROM InventoryB;
BSELECT product_id FROM InventoryA UNION INTERSECT SELECT product_id FROM InventoryB;
CSELECT product_id FROM InventoryA INTERSECT ALL SELECT product_id FROM InventoryB;
DSELECT product_id FROM InventoryA INTERSECT BY SELECT product_id FROM InventoryB;
Attempts:
2 left
💡 Hint
INTERSECT does not combine with UNION or use ALL or BY keywords.
optimization
advanced
2: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?
ASELECT DISTINCT customer_id FROM Orders2023 WHERE customer_id IN (SELECT customer_id FROM Orders2024);
BSELECT customer_id FROM Orders2023 INTERSECT SELECT customer_id FROM Orders2024;
CSELECT customer_id FROM Orders2023 JOIN Orders2024 USING (customer_id);
DSELECT customer_id FROM Orders2023 UNION SELECT customer_id FROM Orders2024;
Attempts:
2 left
💡 Hint
INTERSECT is designed to efficiently find common rows between two queries.
🧠 Conceptual
advanced
2: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 SELECT val FROM A INTERSECT SELECT val FROM B; be?
A[(1), (2), (3)]
B[(1), (1), (2), (2)]
C[(1), (2)]
D[(1), (2), (3), (4)]
Attempts:
2 left
💡 Hint
INTERSECT removes duplicates and returns only common distinct values.
🔧 Debug
expert
3: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;
ABecause the number of columns in both SELECT statements must be the same for INTERSECT.
BBecause INTERSECT cannot be used with SELECT statements containing text columns.
CBecause the column names must be identical in both SELECT statements.
DBecause INTERSECT requires the tables to have the same number of rows.
Attempts:
2 left
💡 Hint
INTERSECT requires matching column counts and compatible types.