Bird
Raised Fist0
DBMS Theoryknowledge~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following best describes the union operation in databases?
easy
A. Finds only the common records between two tables
B. Combines all unique records from two tables
C. Returns records from the first table that are not in the second
D. Deletes duplicate records from a table

Solution

  1. Step 1: Understand the union operation

    The union operation combines all unique records from two sets or tables, removing duplicates.
  2. Step 2: Compare with other operations

    Intersection finds common records, difference finds records in one set but not the other, and deletion removes records.
  3. Final Answer:

    Combines all unique records from two tables -> Option B
  4. Quick Check:

    Union = Combine unique records [OK]
Hint: Union = all unique items combined [OK]
Common Mistakes:
  • Confusing union with intersection
  • Thinking union includes duplicates
  • Mixing difference with union
2. Which SQL keyword is used to find common records between two tables?
easy
A. UNION
B. DIFFERENCE
C. JOIN
D. INTERSECT

Solution

  1. Step 1: Identify the keyword for common records

    The SQL keyword INTERSECT returns only the records common to both tables.
  2. Step 2: Differentiate from other keywords

    UNION combines unique records, DIFFERENCE is not a standard SQL keyword, and JOIN combines rows based on a condition.
  3. Final Answer:

    INTERSECT -> Option D
  4. Quick Check:

    Common records = INTERSECT [OK]
Hint: Common records use INTERSECT keyword [OK]
Common Mistakes:
  • Using UNION instead of INTERSECT
  • Confusing JOIN with INTERSECT
  • Assuming DIFFERENCE is a valid SQL keyword
3. Given two sets A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, what is the result of A INTERSECT B?
medium
A. {3, 4}
B. {1, 2, 3, 4, 5, 6}
C. {1, 2}
D. {5, 6}

Solution

  1. Step 1: Identify common elements in A and B

    The common elements between A and B are 3 and 4.
  2. Step 2: Understand INTERSECT operation

    INTERSECT returns only the common elements, so the result is {3, 4}.
  3. Final Answer:

    {3, 4} -> Option A
  4. Quick Check:

    Intersection = common elements [OK]
Hint: Intersection = only common items [OK]
Common Mistakes:
  • Listing all elements instead of common ones
  • Confusing intersection with union
  • Including elements only in one set
4. Consider the SQL query:
SELECT * FROM Table1
EXCEPT
SELECT * FROM Table2;

What is the purpose of the EXCEPT keyword?
medium
A. Returns records in Table1 but not in Table2
B. Returns records in Table2 but not in Table1
C. Returns records common to both tables
D. Returns all records from both tables

Solution

  1. Step 1: Understand the EXCEPT keyword

    EXCEPT returns records from the first query that are not present in the second query.
  2. Step 2: Apply to given tables

    So, it returns records in Table1 but not in Table2.
  3. Final Answer:

    Returns records in Table1 but not in Table2 -> Option A
  4. Quick Check:

    Difference = records in first not in second [OK]
Hint: EXCEPT = first table minus second table [OK]
Common Mistakes:
  • Confusing EXCEPT with UNION
  • Thinking EXCEPT returns common records
  • Mixing order of tables in EXCEPT
5. You have two tables:
Employees_A with IDs {101, 102, 103, 104} and Employees_B with IDs {103, 104, 105, 106}.
You want to find IDs that are in either table but not in both. Which SQL operation gives this result?
hard
A. FULL OUTER JOIN with WHERE condition
B. INTERSECT
C. SYMMETRIC DIFFERENCE using UNION and EXCEPT
D. UNION

Solution

  1. Step 1: Understand the requirement

    We want IDs in either table but not in both, which is called symmetric difference.
  2. Step 2: Identify SQL approach

    SQL does not have a direct symmetric difference keyword, but it can be done by combining UNION and EXCEPT operations.
  3. Step 3: Explain why other options are incorrect

    UNION returns all unique IDs including common ones, INTERSECT returns only common IDs, FULL OUTER JOIN with WHERE can be complex but not direct symmetric difference.
  4. Final Answer:

    SYMMETRIC DIFFERENCE using UNION and EXCEPT -> Option C
  5. Quick Check:

    Symmetric difference = union minus intersection [OK]
Hint: Symmetric difference = union minus intersection [OK]
Common Mistakes:
  • Using UNION alone includes common IDs
  • Using INTERSECT returns only common IDs
  • Assuming FULL OUTER JOIN alone solves it
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard