Bird
Raised Fist0
DBMS Theoryknowledge~15 mins

Union, intersection, difference in DBMS Theory - Deep Dive

Choose your learning style10 modes available
Overview - Union, intersection, difference
What is it?
Union, intersection, and difference are basic operations used to combine or compare sets of data in databases. Union combines all unique records from two sets. Intersection finds only the records common to both sets. Difference shows records in one set that are not in the other. These operations help organize and analyze data efficiently.
Why it matters
These operations solve the problem of comparing and merging data from different sources or tables. Without them, it would be hard to find commonalities, differences, or combine data without duplicates. This would make data analysis slow, error-prone, and less meaningful, impacting decisions based on incomplete or incorrect data.
Where it fits
Before learning these, you should understand what sets and tables are in databases and basic querying. After mastering these, you can explore more complex operations like joins, subqueries, and advanced filtering to manipulate data across multiple tables.
Mental Model
Core Idea
Union, intersection, and difference are ways to compare and combine groups of data by focusing on all, common, or unique elements respectively.
Think of it like...
Imagine two baskets of fruits: union is putting all fruits from both baskets together without repeats; intersection is picking only the fruits that appear in both baskets; difference is taking fruits from the first basket that are not in the second.
  Set A       Set B
┌───────┐   ┌───────┐
│ 1 2 3 │   │ 2 3 4 │
└───────┘   └───────┘

Union: {1, 2, 3, 4}
Intersection: {2, 3}
Difference (A - B): {1}
Build-Up - 8 Steps
1
FoundationUnderstanding sets and tables
🤔
Concept: Introduce the idea of sets as collections of unique items and how database tables represent sets of rows.
A set is a group of distinct items with no duplicates. In databases, tables store rows of data, which can be thought of as sets of records. Each row is unique based on its content or key. Understanding this helps us apply set operations to tables.
Result
You can now think of database tables as sets, which is essential for applying union, intersection, and difference.
Understanding tables as sets allows you to apply mathematical set operations to data, making complex queries easier to grasp.
2
FoundationBasic SQL SELECT queries
🤔
Concept: Learn how to retrieve data from a single table using SELECT statements.
The SELECT statement fetches rows from a table. For example, SELECT * FROM Employees; returns all employee records. This is the starting point before combining or comparing data from multiple tables.
Result
You can retrieve data from tables, which is necessary before applying union, intersection, or difference.
Knowing how to select data is fundamental because set operations work on the results of these queries.
3
IntermediateUsing UNION to combine data
🤔Before reading on: do you think UNION includes duplicate rows or removes them? Commit to your answer.
Concept: UNION combines results from two queries into one set, removing duplicates.
UNION takes the results of two SELECT queries and merges them into a single result set without duplicates. For example: SELECT name FROM Employees UNION SELECT name FROM Managers; This returns all unique names from both tables.
Result
The output is a combined list of unique records from both queries.
Knowing that UNION removes duplicates helps prevent unexpected repeated data in combined results.
4
IntermediateFinding common data with INTERSECT
🤔Before reading on: does INTERSECT return all rows from both sets or only those present in both? Commit to your answer.
Concept: INTERSECT returns only the rows that appear in both query results.
INTERSECT compares two SELECT query results and returns only the rows found in both. For example: SELECT name FROM Employees INTERSECT SELECT name FROM Managers; This shows names who are both employees and managers.
Result
The output contains only records common to both queries.
Understanding INTERSECT helps you find overlaps in data, useful for identifying shared elements.
5
IntermediateUsing EXCEPT for difference
🤔Before reading on: does EXCEPT return rows in the first set that are also in the second, or only those unique to the first? Commit to your answer.
Concept: EXCEPT returns rows from the first query that are not in the second query.
EXCEPT compares two SELECT queries and returns rows from the first that do not appear in the second. For example: SELECT name FROM Employees EXCEPT SELECT name FROM Managers; This lists employees who are not managers.
Result
The output shows records unique to the first query.
Knowing EXCEPT helps you identify differences between datasets, crucial for filtering out unwanted data.
6
AdvancedHandling duplicates and column matching
🤔Before reading on: do you think UNION requires the same number and type of columns in both queries? Commit to your answer.
Concept: Set operations require queries to have the same number of columns with compatible data types; duplicates are removed by default.
When using UNION, INTERSECT, or EXCEPT, both SELECT queries must return the same number of columns with compatible types. Also, UNION removes duplicates unless UNION ALL is used. For example: SELECT id, name FROM Employees UNION SELECT id, name FROM Managers; This works only if both have two columns with matching types.
Result
Queries run successfully only if column counts and types match; duplicates are handled as per operation.
Understanding these rules prevents errors and unexpected results when combining data.
7
AdvancedPerformance considerations in set operations
🤔Before reading on: do you think UNION is faster than UNION ALL or vice versa? Commit to your answer.
Concept: Set operations can impact query performance; UNION removes duplicates which costs more time than UNION ALL.
UNION removes duplicates by sorting or hashing results, which takes extra time. UNION ALL simply appends results without checking duplicates, making it faster. Choosing the right operation affects speed and resource use, especially on large datasets.
Result
Using UNION ALL improves performance when duplicates are not a concern.
Knowing performance trade-offs helps optimize queries for speed and resource efficiency.
8
ExpertComplex use cases and edge behaviors
🤔Before reading on: do you think NULL values affect set operations like UNION or INTERSECT? Commit to your answer.
Concept: NULL values are treated specially in set operations and can affect results unexpectedly; also, set operations can be combined with other SQL features for complex queries.
In set operations, NULLs are considered equal to each other for duplicate elimination, so rows with NULLs may appear once. Combining set operations with ORDER BY, LIMIT, or joins requires careful syntax and understanding of evaluation order. For example, UNION of queries with NULLs may hide duplicates that differ only in NULL positions.
Result
Results may include or exclude rows with NULLs in ways that surprise beginners; complex queries require precise structure.
Understanding NULL handling and combining set operations with other SQL features prevents subtle bugs and enables advanced data manipulation.
Under the Hood
Set operations work by executing each SELECT query separately, then combining their results in memory or temporary storage. UNION and INTERSECT remove duplicates by sorting or hashing rows, while EXCEPT filters rows from the first set by checking presence in the second. The database engine uses algorithms optimized for these operations, often involving indexes and temporary tables.
Why designed this way?
These operations are based on mathematical set theory, providing a clear and consistent way to combine and compare data. Removing duplicates by default aligns with set behavior, avoiding redundant data. Alternatives like UNION ALL exist for performance when duplicates are acceptable. This design balances correctness, usability, and efficiency.
┌─────────────┐       ┌─────────────┐
│ SELECT A    │       │ SELECT B    │
└─────┬───────┘       └─────┬───────┘
      │                     │
      │                     │
      └───────┬─────────────┘
              │
      ┌───────▼─────────────┐
      │ Set Operation Engine │
      │ (UNION/INTERSECT/    │
      │  EXCEPT logic)       │
      └───────┬─────────────┘
              │
      ┌───────▼─────────────┐
      │ Result Set Output    │
      └─────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does UNION include duplicate rows from both tables? Commit to yes or no.
Common Belief:UNION simply stacks all rows from both tables, including duplicates.
Tap to reveal reality
Reality:UNION removes duplicate rows, returning only unique records from both tables combined.
Why it matters:Assuming duplicates remain can lead to overcounting or incorrect data analysis.
Quick: Does INTERSECT return rows present in either table or only those in both? Commit to your answer.
Common Belief:INTERSECT returns all rows from both tables combined.
Tap to reveal reality
Reality:INTERSECT returns only rows that appear in both tables.
Why it matters:Misunderstanding this causes confusion when trying to find common data, leading to wrong query results.
Quick: Does EXCEPT return rows from both tables or only unique rows from the first? Commit now.
Common Belief:EXCEPT returns all rows from both tables except duplicates.
Tap to reveal reality
Reality:EXCEPT returns only rows from the first table that are not in the second table.
Why it matters:Using EXCEPT incorrectly can exclude important data or include unwanted rows, affecting data integrity.
Quick: Are NULL values treated as equal in set operations? Commit to yes or no.
Common Belief:NULLs are always treated as different values in set operations.
Tap to reveal reality
Reality:In set operations, NULLs are treated as equal for duplicate elimination purposes.
Why it matters:Ignoring this can cause unexpected missing or duplicated rows when NULLs are involved.
Expert Zone
1
Set operations require exact column order and compatible data types; subtle mismatches cause errors that are hard to debug.
2
UNION ALL can be combined with DISTINCT later to control duplicates more flexibly, but this affects performance.
3
NULL handling in set operations can differ between database systems, so cross-platform queries need careful testing.
When NOT to use
Avoid set operations when you need to combine data based on related columns or keys; instead, use JOINs for relational matching. Also, for large datasets where duplicates are acceptable, prefer UNION ALL for better performance.
Production Patterns
In real systems, UNION is used to merge results from similar tables or partitions. INTERSECT helps find overlapping customers or records. EXCEPT is common for identifying missing or unmatched data. These operations are often combined with filters, aggregations, and subqueries for complex reporting.
Connections
Relational Algebra
Set operations in SQL directly implement relational algebra operations.
Understanding relational algebra clarifies why these operations behave as they do and how they fit into database theory.
Venn Diagrams
Set operations correspond to areas in Venn diagrams representing sets and their overlaps.
Visualizing data sets as Venn diagrams helps grasp the logic of union, intersection, and difference intuitively.
Boolean Logic
Set operations mirror Boolean logic operations: OR (union), AND (intersection), and NOT (difference).
Recognizing this connection aids in understanding how queries filter and combine data based on conditions.
Common Pitfalls
#1Using UNION when duplicates are acceptable, causing unnecessary performance cost.
Wrong approach:SELECT name FROM Employees UNION SELECT name FROM Managers;
Correct approach:SELECT name FROM Employees UNION ALL SELECT name FROM Managers;
Root cause:Misunderstanding that UNION removes duplicates and is slower than UNION ALL.
#2Mismatched columns in set operations causing errors.
Wrong approach:SELECT id, name FROM Employees UNION SELECT name FROM Managers;
Correct approach:SELECT id, name FROM Employees UNION SELECT id, name FROM Managers;
Root cause:Not ensuring both queries have the same number and type of columns.
#3Assuming INTERSECT returns all rows from both tables.
Wrong approach:SELECT name FROM Employees INTERSECT SELECT name FROM Managers; -- expecting all names combined
Correct approach:SELECT name FROM Employees INTERSECT SELECT name FROM Managers; -- returns only common names
Root cause:Confusing INTERSECT with UNION behavior.
Key Takeaways
Union, intersection, and difference are fundamental set operations that help combine and compare data in databases.
Union merges unique records from two sets, intersection finds common records, and difference shows unique records from one set.
These operations require queries to have matching columns and handle duplicates and NULLs carefully.
Choosing the right operation and understanding their performance impact is key to efficient data querying.
Misunderstanding these concepts leads to incorrect data results and inefficient queries, so mastering them is essential for database work.

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