Set Union, Intersection, and Difference: Complete Guide
Set operations form the mathematical language of data structures, database queries (SQL joins), and probability theory.
Fundamental Operations Explained
- 1Union ($A \cup B$): The set of all elements that belong to $A$, $B$, or both:
A \cup B = \{x \mid x \in A \lor x \in B\}
- 1Intersection ($A \cap B$): The set of elements that belong to both $A$ and $B$:
A \cap B = \{x \mid x \in A \land x \in B\}
- 1Relative Complement / Set Difference ($A \setminus B$): Elements in $A$ that are not in $B$:
A \setminus B = \{x \mid x \in A \land x \notin B\}
- 1Symmetric Difference ($A \Delta B$): Elements in either $A$ or $B$, but not in their intersection:
A \Delta B = (A \setminus B) \cup (B \setminus A) = (A \cup B) \setminus (A \cap B)
The Principle of Inclusion-Exclusion
To calculate the total size of the union without double-counting common elements:
|A \cup B| = |A| + |B| - |A \cap B|
Set Operations Matrix
| Operation | Symbol | SQL Equivalent | Venn Diagram Region |
|---|---|---|---|
| Union | $A \cup B$ | FULL OUTER JOIN | Both circles combined |
| Intersection | $A \cap B$ | INNER JOIN | Central overlap football |
| Difference | $A \setminus B$ | LEFT JOIN ... WHERE B IS NULL | Crescent moon of A |
| Symmetric Diff | $A \Delta B$ | Outer union minus inner | Outer crescents of both |