0
0
DBMS Theoryknowledge~20 mins

Union, intersection, difference in DBMS Theory - Practice Problems & Coding Challenges

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
Understanding UNION operation in SQL

Consider two tables, TableA and TableB, each with a single column value. TableA contains values (1, 2, 3) and TableB contains values (3, 4, 5). What will be the result of the following SQL query?

SELECT value FROM TableA
UNION
SELECT value FROM TableB;
DBMS Theory
SELECT value FROM TableA
UNION
SELECT value FROM TableB;
A(1, 2, 4, 5)
B(1, 2, 3, 4, 5)
C(1, 2, 3, 3, 4, 5)
D(3)
Attempts:
2 left
💡 Hint

Remember that UNION removes duplicate rows.

🧠 Conceptual
intermediate
2:00remaining
Understanding INTERSECT operation in SQL

Given the same tables TableA (values 1, 2, 3) and TableB (values 3, 4, 5), what is the output of this query?

SELECT value FROM TableA
INTERSECT
SELECT value FROM TableB;
DBMS Theory
SELECT value FROM TableA
INTERSECT
SELECT value FROM TableB;
A(3)
B(1, 2, 3, 4, 5)
C(1, 2)
D(4, 5)
Attempts:
2 left
💡 Hint

INTERSECT returns only the common rows between both tables.

🧠 Conceptual
advanced
2:00remaining
Understanding EXCEPT (Difference) operation in SQL

Using the same tables TableA (values 1, 2, 3) and TableB (values 3, 4, 5), what does this query return?

SELECT value FROM TableA
EXCEPT
SELECT value FROM TableB;
DBMS Theory
SELECT value FROM TableA
EXCEPT
SELECT value FROM TableB;
A(1, 2, 3, 4, 5)
B(4, 5)
C(3, 4, 5)
D(1, 2)
Attempts:
2 left
💡 Hint

EXCEPT returns rows from the first table that are not in the second table.

🔍 Analysis
advanced
2:00remaining
Comparing UNION ALL and UNION

What is the difference in output between these two queries given TableA (1, 2, 3) and TableB (3, 4, 5)?

Query 1:
SELECT value FROM TableA
UNION
SELECT value FROM TableB;

Query 2:
SELECT value FROM TableA
UNION ALL
SELECT value FROM TableB;
DBMS Theory
SELECT value FROM TableA
UNION
SELECT value FROM TableB;

SELECT value FROM TableA
UNION ALL
SELECT value FROM TableB;
AQuery 1 removes duplicates; Query 2 includes duplicates
BQuery 1 includes duplicates; Query 2 removes duplicates
CBoth queries return the same result
DQuery 1 returns only TableA values; Query 2 returns only TableB values
Attempts:
2 left
💡 Hint

Think about how UNION and UNION ALL treat duplicate rows.

Reasoning
expert
2:00remaining
Determining the number of rows after set operations

Given two tables, TableX with 5 unique rows and TableY with 7 unique rows, where 3 rows are common to both tables, how many rows will the following query return?

SELECT * FROM TableX
INTERSECT
SELECT * FROM TableY;
DBMS Theory
SELECT * FROM TableX
INTERSECT
SELECT * FROM TableY;
A5
B9
C3
D7
Attempts:
2 left
💡 Hint

INTERSECT returns only rows present in both tables.