In the previous article we saw what a set is and how to define its elements precisely. Now we take one step further: we will learn to operate on sets (combine them, find what they have in common, subtract one from another) and to visualize them with Venn diagrams.
These operations show up everywhere: in queries written in SQL (Structured
Query Language), in data filters, and in programming logic. For example, a
predicate such as WHERE a IN (SELECT ...) AND b NOT IN (...) already
combines the inclusion and exclusion of sets.
Practical reading: databases and search
In relational databases, UNION, INTERSECT, and EXCEPT correspond,
respectively, to union, intersection, and difference (see the
PostgreSQL documentation
on these operators). In search engines,
OR widens the result set, AND narrows it, and exclusion with -
removes the unwanted terms.
The Universal Set and Venn Diagrams #
The universal set \(U\) is the set that contains all the objects relevant in the context of a problem. Every set in the problem is a subset of \(U\).
When we write \(A = \{x \in \mathbb{N} \mid x \leq 50\}\), we are saying that \(U = \mathbb{N}\) and we select within \(U\) only the elements that satisfy the property. Stating the universe explicitly avoids ambiguity.
Venn diagrams are the geometric representation of this idea:
- The universe \(U\) is represented by the outer rectangle
- Each subset is represented by a circular region inside the rectangle
- The relative position of the circles indicates the relationships between the sets
The figure below shows the most elementary case: \(A \subset U\). The circle of \(A\) lies entirely inside the rectangle \(U\): every element of \(A\) belongs to \(U\), but \(U\) contains elements outside \(A\).

The diagrams are named in honor of the English mathematician John Venn (1834 to 1923), who introduced them in 1880.
More than a century before Venn, Leonhard Euler already used circles to represent relationships between classes, in the Letters to a German Princess, written between 1760 and 1762 and published in 1768. The two drawings follow different rules:
- An Euler diagram shows only the regions that can have elements. If \(A \subset U\), the circle of \(A\) lies inside the rectangle; if two sets are disjoint, their circles do not even touch.
- A Venn diagram, in the strict sense, draws all the combinations of membership (\(2^2 = 4\) regions for two sets, \(2^3 = 8\) for three), including the empty ones. What is known about each region is indicated by shading or marking it.
In current usage, even in mathematics books, “Venn diagram” has become the name for any drawing of this kind, and this article uses the term in that broad sense. Strictly speaking, the figure above and the figures for inclusion and disjoint sets throughout the text are Euler diagrams.
The figure below is a Venn diagram in the strict sense, for three sets. The eight regions always appear, even if some are empty, and each one corresponds to a combination of membership in \(A\), \(B\), and \(C\); the color indicates how many of the three sets the region is in. This numbering returns in Exercise 3 and in the next article, when we count the elements of a union of three sets.

Example: inclusion in a diagram
Consider \(U = \mathbb{N}\), \(A = \{x \in \mathbb{N} \mid 10 \leq x \leq 100\}\), and \(B = \{x \in \mathbb{N} \mid 15 \leq x \leq 50\}\).
Since every element of \(B\) also belongs to \(A\), we have \(B \subset A \subset U\). In a Venn diagram, the circle of \(B\) lies entirely inside the circle of \(A\), which in turn is inside the rectangle \(U\):

The Four Fundamental Operations #
Union, intersection, difference, and complement are the basic operations of the algebra of sets (see the Wikipedia entry Set operations (Boolean)). For each one, we will see the definition, the diagram, examples, and the SQL correspondence.
Union #
The union of \(A\) and \(B\) gathers all the elements that belong to at least one of the two sets:
$$A \cup B = \{x \in U \mid x \in A\ \text{or}\ x \in B\}$$In the Venn diagram, the union corresponds to the entire area covered by the two circles.

Example with elements
If \(A = \{1,2,3,4,5,6\}\) and \(B = \{5,6,7,8,9\}\), then the elements \(5\) and \(6\) lie in the intersection \(A \cap B\), while \(1,2,3,4\) lie only in \(A\) and \(7,8,9\) only in \(B\). The union joins all these regions, resulting in:
$$A \cup B = \{1,2,3,4,5,6,7,8,9\}$$
Example without intersection (disjoint sets)
If \(A = \{1,2,3\}\) and \(B = \{7,8,9\}\), then \(A \cap B = \emptyset\): the circles are isolated in the universe \(U\), with no overlap. Even so, the union is made up of all the elements that are in \(A\) or in \(B\):
$$A \cup B = \{1,2,3,7,8,9\}$$
Properties:
| Property | Statement |
|---|---|
| Idempotence | \(A \cup A = A\) |
| Identity element | \(A \cup \emptyset = A\) |
| Absorption by the universe | \(A \cup U = U\) |
| Commutativity | \(A \cup B = B \cup A\) |
| Associativity | \((A \cup B) \cup C = A \cup (B \cup C)\) |
| Inclusion in the union | \(A \subseteq A \cup B\) |
| Equivalence (characteristic) | \(A \subseteq B \iff A \cup B = B\) |
Special case: if \(W \subseteq Z\), then \(Z \cup W = Z\).
Applied example (minimum height)
Consider, in a universe \(U\) of measured people:
$$Z = \{x \in U \mid \text{height}(x) \geq 1.75\text{ m}\}$$$$W = \{x \in U \mid \text{height}(x) \geq 1.90\text{ m}\}$$
Every element of \(W\) also belongs to \(Z\), so \(W \subseteq Z\). Therefore:
$$Z \cup W = Z$$
Application in SQL
In SQL, the union corresponds to the UNION operator, which combines the
results of two queries, eliminating duplicates. The query below returns all
the customers from SP or RJ, that is, \(A \cup B\):
SELECT id FROM customers WHERE state = 'SP'
UNION
SELECT id FROM customers WHERE state = 'RJ'Intersection #
The intersection of \(A\) and \(B\) gathers only the elements that belong to both simultaneously:
$$A \cap B = \{x \in U \mid x \in A\ \text{and}\ x \in B\}$$In the Venn diagram, the intersection is the overlapping area of the two circles.

Example with elements
If \(A = \{1,2,3,4,5,6\}\) and \(B = \{5,6,7,8,9\}\), only the elements common to both sets enter the intersection:
$$A \cap B = \{5,6\}$$
Example without intersection (disjoint sets)
If \(A = \{1,2,3\}\) and \(B = \{7,8,9\}\), the sets share no elements, so:
$$A \cap B = \emptyset$$
Properties:
| Property | Statement |
|---|---|
| Idempotence | \(A \cap A = A\) |
| Intersection with the empty set | \(A \cap \emptyset = \emptyset\) |
| Intersection with the universe | \(A \cap U = A\) |
| Commutativity | \(A \cap B = B \cap A\) |
| Inclusion of the intersection | \(A \cap B \subseteq A\) |
| Equivalence (characteristic) | \(A \subseteq B \iff A \cap B = A\) |
\(A\) and \(B\) are disjoint when \(A \cap B = \emptyset\): they share no element.
Special case: if \(W \subseteq Z\), then \(Z \cap W = W\).
Applied example (minimum height)
Consider, in a universe \(U\) of measured people:
$$Z = \{x \in U \mid \text{height}(x) \geq 1.75\text{ m}\}$$$$W = \{x \in U \mid \text{height}(x) \geq 1.90\text{ m}\}$$
Since \(W \subseteq Z\), every element of \(W\) is already in \(Z\). Therefore:
$$Z \cap W = W$$
Application in SQL
The intersection corresponds to the INTERSECT operator. The query below
returns only the customers who bought in both January and February, that is,
\(A \cap B\):
SELECT customer_id FROM sales WHERE month = 1
INTERSECT
SELECT customer_id FROM sales WHERE month = 2Difference #
The difference \(A - B\) (read “\(A\) minus \(B\)”) contains the elements of \(A\) that are not in \(B\):
$$A - B = \{x \in U \mid x \in A\ \text{and}\ x \notin B\}$$In the Venn diagram, it is the part of \(A\) that does not overlap \(B\). Note that the operation is not commutative: \(A - B \neq B - A\) in general.
In the literature, this operation also appears as the relative complement of \(B\) in \(A\).

Example with elements (both directions)
If \(A = \{1,2,3,4,5,6\}\) and \(B = \{5,6,7,8,9\}\):
$$A - B = \{1,2,3,4\}$$$$B - A = \{7,8,9\}$$


Example without intersection (disjoint sets)
If \(A = \{1,2,3\}\) and \(B = \{7,8,9\}\), then \(A \cap B = \emptyset\), and:
$$A - B = \{1,2,3\} = A$$$$B - A = \{7,8,9\} = B$$
When the sets are disjoint, the subtrahend removes no element from the minuend, so the difference is always equal to the minuend itself (the left side of the operation): \(A - B = A\) and \(B - A = B\). In the diagrams, the highlighted region is the entire circle of the minuend:


Properties:
| Property | Statement |
|---|---|
| Difference with itself | \(A - A = \emptyset\) |
| Removing the empty set | \(A - \emptyset = A\) |
| Empty set as minuend | \(\emptyset - A = \emptyset\) |
| Equivalence (characteristic) | \(A \subseteq B \iff A - B = \emptyset\) |
| Inclusion of the difference | \(A - B \subseteq A\) |
Useful relation: \(A - B = A \cap \bar{B}\) (we will see the complement next).
If \(W \subseteq Z\), then \(W - Z = \emptyset\).
Applied example (minimum height, both directions)
Consider, in a universe \(U\) of measured people:
$$Z = \{x \in U \mid \text{height}(x) \geq 1.75\text{ m}\}$$$$W = \{x \in U \mid \text{height}(x) \geq 1.90\text{ m}\}$$
Since \(W \subseteq Z\), we have:
$$Z - W = \{x \in U \mid 1.75 \leq \text{height}(x) < 1.90\}$$$$W - Z = \emptyset$$


Application in SQL
The difference corresponds to the EXCEPT operator. A classic use is
identifying records present in one table but absent from another, such as
users who signed up but have not yet made any purchase
(\(A - B\)):
SELECT id FROM users
EXCEPT
SELECT DISTINCT user_id FROM ordersCombining the two directions of the difference (\(A - B\) and \(B - A\)) we obtain the symmetric difference, which retains only the elements exclusive to each set. We explore this operation in Exercise 2.
Complement #
The complement of \(A\) with respect to \(U\) is everything that is in \(U\) but not in \(A\):
$$\bar{A} = U - A = \{x \in U \mid x \notin A\}$$In the Venn diagram, the complement is the entire area of the rectangle that lies outside the circle of \(A\):

Properties:
| Property | Statement |
|---|---|
| Involution | \(\overline{\bar{A}} = A\) |
| Complementarity | \(A \cup \bar{A} = U\) |
| Disjointness | \(A \cap \bar{A} = \emptyset\) |
| Complement of the universe | \(\bar{U} = \emptyset\) |
| Complement of the empty set | \(\bar{\emptyset} = U\) |
Examples of complement
- \(U = \mathbb{N}\), \(A = \{x \in \mathbb{N} \mid x \leq 50\}\) → \(\bar{A} = \{x \in \mathbb{N} \mid x > 50\}\)
- \(U = \mathbb{Z}\), \(A = \{x \in \mathbb{Z} \mid x > 2\}\) → \(\bar{A} = \{\ldots, -2, -1, 0, 1, 2\}\)
- \(U = \mathbb{N}\), \(A = \{x \in \mathbb{N} \mid x > 2\}\) → \(\bar{A} = \{1, 2\}\)
Application in SQL
The complement appears whenever a subset is excluded from the universe. The query below returns all the users without an active subscription, that is, the complement of the set of subscribers:
SELECT id FROM users
WHERE id NOT IN (SELECT user_id FROM subscriptions WHERE active = TRUE)If user_id can be NULL in subscriptions, the NOT IN returns no
rows at all and stops being the complement. In that case, use NOT EXISTS.
Fundamental Identities #
De Morgan’s Laws #
De Morgan’s laws describe how the complement distributes over union and intersection. They are widely used in logic and programming, and if you have read the article on Boolean Algebra, you have already met them in the context of the logical operations AND, OR, and NOT. Here, the identities are expressed in terms of sets:
$$\overline{A \cup B} = \bar{A} \cap \bar{B}$$$$\overline{A \cap B} = \bar{A} \cup \bar{B}$$In words: the complement of a union is the intersection of the complements;
the complement of an intersection is the union of the complements. You use
this when negating compound conditions: NOT (A OR B) is equivalent to
NOT A AND NOT B.
Example of logical refactoring
Imagine a rule that grants access to whoever is an admin or has an active account and blocks everyone else. Written directly, the blocking condition is the negation of the access condition:
if not (is_admin or is_active):
block_access()By De Morgan’s First Law, the same condition can be rewritten as “is not an admin and the account is not active”:
if not is_admin and not is_active:
block_access()The two forms are logically equivalent. The first makes the access rule being negated visible; the second lists, one by one, the conditions that lead to blocking. Which one reads better depends on how the rule was conceived.
The figures below show each identity in steps, separately:


Distributivity #
$$A \cap (B \cup C) = (A \cap B) \cup (A \cap C)$$$$A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$$Intersection distributes over union, and vice versa, exactly as multiplication distributes over addition in the numbers.
The following figures show, in steps, the two distributivity equalities:


Proof of the second identity \(A \cup (B \cap C) = (A \cup B) \cap (A \cup C)\):
Part 1: Let us show that \(A \cup (B \cap C) \subseteq (A \cup B) \cap (A \cup C)\).
Let \(x \in A \cup (B \cap C)\). Then either \(x \in A\), or \(x \in B \cap C\) (that is, \(x \in B\) and \(x \in C\)). In both cases, \(x \in A \cup B\) and \(x \in A \cup C\) simultaneously. Hence \(x \in (A \cup B) \cap (A \cup C)\). \(\square\)
Part 2: Let us show the opposite inclusion.
Let \(x \in (A \cup B) \cap (A \cup C)\). Then \(x \in A \cup B\) and \(x \in A \cup C\). If \(x \in A\), we are done. Otherwise, to belong to \(A \cup B\) without being in \(A\), we must have \(x \in B\); and to belong to \(A \cup C\) without being in \(A\), we must have \(x \in C\). Therefore \(x \in B \cap C\), and so \(x \in A \cup (B \cap C)\). \(\square\)
The first identity (\(A \cap (B \cup C) = (A \cap B) \cup (A \cap C)\)) follows by an analogous argument, swapping the roles of \(\cup\) and \(\cap\). Try proving it as an exercise.
Absorption #
$$A \cup (A \cap B) = A \qquad \text{and} \qquad A \cap (A \cup B) = A$$Other Useful Identities #
$$A - B = A \cap \bar{B}$$Proof: \(x \in A - B\) if and only if \(x \in A\) and \(x \notin B\), which is equivalent to \(x \in A\) and \(x \in \bar{B}\), that is, \(x \in A \cap \bar{B}\). \(\square\)
Example
If \(U = \{1,2,3,4,5\}\), \(A = \{1,2,3\}\), and \(B = \{2,3,4\}\), then \(\bar{B} = \{1,5\}\), and:
$$A - B = \{1\} = A \cap \bar{B} = \{1,2,3\} \cap \{1,5\} = \{1\}$$If \(A \cap B = \emptyset\): \(A - B = A\) and \(B - A = B\).
Summary Table #
| Property | Union | Intersection |
|---|---|---|
| Commutativity | \(A \cup B = B \cup A\) | \(A \cap B = B \cap A\) |
| Associativity | \((A \cup B) \cup C = A \cup (B \cup C)\) | \((A \cap B) \cap C = A \cap (B \cap C)\) |
| Distributivity | \(A \cup (B \cap C) = (A \cup B) \cap (A \cup C)\) | \(A \cap (B \cup C) = (A \cap B) \cup (A \cap C)\) |
| De Morgan | \(\overline{A \cup B} = \bar{A} \cap \bar{B}\) | \(\overline{A \cap B} = \bar{A} \cup \bar{B}\) |
| Absorption | \(A \cup (A \cap B) = A\) | \(A \cap (A \cup B) = A\) |
Exercises #
Exercise 1: Concrete operations
Let \(U = \{0,1,2,3,4\}\), \(A = \{0,4\}\), \(B = \{0,1,2,3\}\), \(C = \{1,4\}\), \(D = \{0,1\}\).
Determine: (a) \(A \cup B\) (b) \(B \cap C\) (c) \(A \cap B\) (d) \(A \cup (B \cap C)\) (e) \((A \cup B) \cap (A \cup C)\)
(f) \(\overline{(A \cap B)} \cup \overline{(A \cap C)}\) (g) \(\overline{A \cup B}\) (h) \(A - B\) (i) \(B - A\) (j) \(A \cup (B \cap C \cap D)\)
Solutions
(a) \(A \cup B = \{0,1,2,3,4\} = U\)
(b) \(B \cap C = \{1\}\)
(c) \(A \cap B = \{0\}\)
(d) \(A \cup (B \cap C) = \{0,4\} \cup \{1\} = \{0,1,4\}\)
(e) \((A \cup B) \cap (A \cup C) = U \cap \{0,1,4\} = \{0,1,4\}\). Note that it equals (d), confirming distributivity.
(f) \(\overline{A \cap B} = U - \{0\} = \{1,2,3,4\}\); \(\overline{A \cap C} = U - \{4\} = \{0,1,2,3\}\); hence the union is \(\{0,1,2,3,4\} = U\).
(g) \(A \cup B = U\), therefore \(\overline{A \cup B} = \emptyset\).
(h) \(A - B = \{4\}\) (0 is in \(B\), 4 is not)
(i) \(B - A = \{1,2,3\}\)
(j) \(B \cap C \cap D = \{0,1\} \cap \{1\} = \{1\}\) (intersection of \(B\), \(C\), and \(D\)), so \(A \cup \{1\} = \{0,1,4\}\).
Exercise 2: Symmetric difference
The symmetric difference \(A \mathbin{\Delta} B\) is defined as:
$$A \mathbin{\Delta} B := (A - B) \cup (B - A)$$Interpret this operation geometrically and determine \(A \mathbin{\Delta} B\) for \(A = \{0,4\}\) and \(B = \{0,1,2,3\}\).
Solution
The symmetric difference retains the elements that belong to exactly one of the two sets, without the intersection. Visually, in the Venn diagram, it is the area of the two circles without the overlap. In Boolean language, this corresponds to the XOR operator (or “exclusive or”, \(\oplus\)); if you want to review this connection, it is worth going back to the article on Boolean Algebra.
A useful visual analogy: git diff highlights removed lines as \(A - B\),
added lines as \(B - A\), and, together, everything that changed as
\(A \mathbin{\Delta} B\). It is only an analogy: diff preserves order and
context.
\(A - B = \{4\}\), \(B - A = \{1,2,3\}\), therefore \(A \mathbin{\Delta} B = \{1,2,3,4\}\).

In the following solutions, the colors are merely illustrative, to highlight the relevant regions in each case. It is a visual aid, not a formal convention.
Exercise 3: Diagrams with three sets
Describe what the Euler diagram of each situation would look like (the drawing that shows only the regions that can have elements):
(i) \(A \subset B \subset C\)
(ii) \(A \cap B = \emptyset\), \(A \cap C = \emptyset\), \(B \cap C = \emptyset\)
(iii) \(A \subseteq B \cup C\)
(iv) \(A \subseteq \bar{B}\)
(v) \(A \subseteq B - C\)
Solutions
The following five figures are Euler diagrams, and each one omits the regions that the condition forces to be empty.
(i) Three concentric circles: \(A\) innermost, \(B\) in the middle, \(C\) outermost.

(ii) Three completely disjoint circles inside the rectangle \(U\): none touches another.

(iii) The circle of \(A\) lies entirely inside the region covered by \(B \cup C\). Part of \(A\) can be in \(B\), part in \(C\), part in the intersection, but no part of \(A\) lies outside \(B \cup C\).

(iv) \(A \subseteq \bar{B}\) means that no element of \(A\) is in \(B\): the circles of \(A\) and \(B\) are disjoint.

(v) \(A \subseteq B - C\) means that \(A\) is in the part of \(B\) that does not belong to \(C\): the circle of \(A\) is inside \(B\) and completely outside \(C\).

The same situations in Venn diagrams. In the strict sense, the eight regions of three sets are always drawn, and those that the condition forces to be empty appear hatched in the figure below. In (i), for example, \(A \subset B\) empties everything that is in \(A\) and outside \(B\) (regions 1 and 5), and \(B \subset C\) empties everything that is in \(B\) and outside \(C\) (regions 2 and 4). The hatching only records what must be empty: the fact that the inclusions are strict (there being an element of \(B\) outside \(A\) and of \(C\) outside \(B\)) does not appear in the drawing.

Next Steps #
With the operations mastered, the next article deals with one question: how many elements does a set formed from operations have? The Inclusion-Exclusion Principle answers precisely that, and it is the gateway to combinatorial counting.
See you then!