Sets and set operations
Unordered collections of distinct things, and the operations β union, intersection, difference β your database already runs.
14 cards Β· 8 quiz questions Β· 10 min read
Ask a database for all customers who bought in January or February and you get a union. Ask for those who bought in both and you get an intersection. Ask for January buyers who did not return in February and you get a difference. Set theory is not a topic that happens to resemble what your queries do β it is the thing your queries do, formalised in the 1870s by Georg Cantor and given a compact notation that has never needed replacing.
What a set is, and what it forgets
A set is an unordered collection of distinct objects, called its elements or members. Two words there do the work.
Unordered: position carries no information. Distinct: repetition carries none either. So {1, 2, 3}, {3, 1, 2} and {1, 2, 2, 3} are not three sets that happen to be similar β they are the same set, written three ways. Two sets are equal exactly when they have the same members, which is the axiom of extensionality: a set is nothing more than what is in it.
The only primitive relation is membership. Write x β A for βx is an element of Aβ, and x β A for βx is notβ. Everything else is built from that one idea.
Sets are written two ways. Roster notation lists them: {2, 3, 5, 7}. Set-builder notation gives a rule: {x | x is prime and x < 10}, read βthe set of all x such that β¦β. The vertical bar is βsuch thatβ, and sometimes a colon is used instead. Builder notation is not just a convenience β an infinite set like {x | x is even} cannot be listed at all.
The empty set
β
is the set with no elements, and there is only one of it. Its cardinality is zero. It is also where most peopleβs first mistake lives: β
and {β
} are different. The second is a set with one element, and that element happens to be the empty set. An empty box and a box containing an empty box are not the same box.
Subsets
A β B means every element of A is also in B. Formally: x β A implies x β B, which makes β a conditional in disguise. Two consequences follow immediately from that reading:
- Every set is a subset of itself. The implication holds trivially.
βis a subset of every set. With no elements, no counterexample can exist β the same vacuous truth that makes an empty array pass anevery()check.
A proper subset is a subset that leaves something out: A β B when A β B and A β B. Be a little careful in the wild, because some authors use β for plain subset; β is the unambiguous symbol when the distinction matters.
Subsets also give the standard way to prove two sets equal: show A β B and B β A. Double inclusion forces equality, and it is the set-theoretic mirror of proving a biconditional in logic.
The operations
Each operation on sets is a logical connective applied to membership.
- Union
A βͺ B={x | x β A β¨ x β B}β in either, or both. Corresponds to or. - Intersection
A β© B={x | x β A β§ x β B}β in both. Corresponds to and. Sets with empty intersection are disjoint. - Difference
A \ Bβ in A but not in B. Also writtenA β B. - Complement
A'=U \ Aβ everything in the universal setUthat is not in A. Corresponds to not, and it is meaningless until you say what the universe is. - Symmetric difference
A Ξ B=(A βͺ B) \ (A β© B)β in exactly one of them. This is exclusive-or, and it is what a diff between two collections reports.
Because the operations are connectives, the logical laws carry straight across. De Morgan holds: the complement of a union is the intersection of the complements. Distribution holds: A β© (B βͺ C) = (A β© B) βͺ (A β© C). Draw two or three overlapping circles and the identities become visible rather than memorised β that is the whole purpose of a Venn diagram.
Counting: cardinality and the power set
The cardinality |A| is the number of elements. For finite sets, the inclusion-exclusion principle says |A βͺ B| = |A| + |B| β |A β© B|: adding the two counts double-counts the overlap, so you subtract it once. If 12 people know Python, 9 know SQL, and 4 know both, then 17 know at least one, not 21.
The power set P(A) is the set of all subsets of A, including β
and A itself. For A = {a, b}, the power set is {β
, {a}, {b}, {a, b}}. If |A| = n then |P(A)| = 2^n, because building a subset means making n independent in-or-out decisions. Each subset is a bitmask β which is exactly how permission flags work.
Finally, the Cartesian product A Γ B is the set of ordered pairs (a, b). Here order does matter, so it produces a different kind of object; its size is |A| Γ |B|, and it is precisely what a SQL CROSS JOIN returns.
Where the notation lands in code
Sets are a first-class data structure, and the operations map almost symbol for symbol.
const a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
a.has(3); // membership: 3 β A
new Set([...a, ...b]); // union β 1,2,3,4
[...a].filter(x => b.has(x)); // intersection β 3
[...a].filter(x => !b.has(x)); // difference β 1,2
[...new Set(listWithDupes)]; // deduplicate
SELECT id FROM jan UNION SELECT id FROM feb; -- A βͺ B
SELECT id FROM jan INTERSECT SELECT id FROM feb; -- A β© B
SELECT id FROM jan EXCEPT SELECT id FROM feb; -- A \ B
Python spells the same operations with operators: a | b, a & b, a - b, a ^ b, plus a <= b for subset. Tag filtering is intersection (βshow posts tagged both rust and asyncβ). An inner join is an intersection on key values; deduplication is just building a set.
The one habit worth forming is choosing the structure deliberately. A set answers βis this present?β in roughly constant time and forgets order and duplicates; a list answers βwhat is at position three?β and remembers both. Storing a ranked top-five in a set silently destroys the ranking. Storing a permission list in an array invites duplicates and slow lookups. Knowing which properties you actually need is the practical payoff of knowing what a set is.
Which of these is a different set from the other three?
Sources
- Paul R. Halmos β Naive Set Theory book The classic short introduction to sets, written for readers who want the ideas before the axioms.
- Kenneth H. Rosen β Discrete Mathematics and Its Applications book Chapter 2 covers sets, set operations, cardinality and power sets with computing examples.
- Herbert B. Enderton β Elements of Set Theory book A rigorous development for readers who want the axiomatic version after the naive one.
- MDN Web Docs β Set website Reference for the JavaScript Set object used as the programming counterpart here.