0
0
SQLquery~20 mins

Why set operations are needed in SQL - 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!
🧠 Conceptual
intermediate
2:00remaining
Purpose of SQL Set Operations
Why do we use set operations like UNION, INTERSECT, and EXCEPT in SQL?
ATo update multiple rows in a table simultaneously
BTo delete duplicate rows within a single table
CTo create new tables automatically from existing ones
DTo combine or compare results from multiple queries efficiently
Attempts:
2 left
💡 Hint
Think about how you might want to merge or find common data from two lists.
query_result
intermediate
2: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
AAlice, Bob, Charlie, Diana, Eve (no duplicates)
BAlice, Bob, Charlie, Bob, Diana, Eve (duplicates included)
COnly names present in both tables: Bob
DOnly names present in Employees_A but not in Employees_B
Attempts:
2 left
💡 Hint
UNION removes duplicates by default.
📝 Syntax
advanced
2: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'?
ASELECT id FROM Table1 WHERE id INTERSECT SELECT id FROM Table2;
BSELECT id FROM Table1 UNION INTERSECT SELECT id FROM Table2;
CSELECT id FROM Table1 INTERSECT SELECT id FROM Table2;
DSELECT id FROM Table1 INTERSECT WHERE id IN Table2;
Attempts:
2 left
💡 Hint
INTERSECT is used between two SELECT statements.
optimization
advanced
2: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?
AUsing UNION in SQL to combine customer IDs is more efficient because it reduces data transfer and leverages database optimization.
BJOIN is best because it combines tables row by row.
CRunning two separate queries and merging in application is better for performance.
DUsing subqueries with EXISTS is always faster than set operations.
Attempts:
2 left
💡 Hint
Think about where it's best to combine data: in the database or outside.
🔧 Debug
expert
3: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;
AEXCEPT requires a WHERE clause to work properly.
BColumn order and types must match exactly in set operations; here, Table2 columns are selected in wrong order.
CTable2 does not exist in the database.
DEXCEPT cannot be used with more than one column.
Attempts:
2 left
💡 Hint
Check the order and types of columns in both SELECT statements.