Challenge - 5 Problems
Set Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of SQL Set Operations
Why do we use set operations like UNION, INTERSECT, and EXCEPT in SQL?
Attempts:
2 left
💡 Hint
Think about how you might want to merge or find common data from two lists.
✗ Incorrect
Set operations allow combining or comparing results from different queries, like merging lists or finding common or different items.
❓ query_result
intermediate2:00remaining
Result of UNION Operation
Given two tables, Employees_A and Employees_B, each with a column 'Name', what will this query return?
SELECT Name FROM Employees_A
UNION
SELECT Name FROM Employees_B;
SQL
Employees_A: Name Alice Bob Charlie Employees_B: Name Bob Diana Eve
Attempts:
2 left
💡 Hint
UNION removes duplicates by default.
✗ Incorrect
UNION combines rows from both queries and removes duplicates, so Bob appears only once.
📝 Syntax
advanced2:00remaining
Correct Syntax for INTERSECT
Which of the following SQL queries correctly uses INTERSECT to find common rows between two tables, Table1 and Table2, both having a column 'id'?
Attempts:
2 left
💡 Hint
INTERSECT is used between two SELECT statements.
✗ Incorrect
Option C correctly places INTERSECT between two SELECT queries. Other options misuse INTERSECT or have syntax errors.
❓ optimization
advanced2:00remaining
Optimizing Queries Using Set Operations
You want to find all customers who have placed orders or have signed up for newsletters. Which SQL approach is more efficient and why?
Attempts:
2 left
💡 Hint
Think about where it's best to combine data: in the database or outside.
✗ Incorrect
UNION lets the database combine results efficiently, reducing data sent to the application and using indexes.
🔧 Debug
expert3:00remaining
Why Does This EXCEPT Query Fail?
Consider these two tables with different column orders:
Table1: (id INT, name VARCHAR)
Table2: (name VARCHAR, id INT)
Why does this query cause an error?
SELECT id, name FROM Table1
EXCEPT
SELECT name, id FROM Table2;
Attempts:
2 left
💡 Hint
Check the order and types of columns in both SELECT statements.
✗ Incorrect
Set operations require the same number of columns with matching types and order. Table2 selects columns in different order causing a type mismatch error.