↓ Skip to main content

Venn Diagrams and Set Operations

·3803 words·18 mins·
 Author
Author
Francisco Bustamante
Chemist, data scientist and Python programmer.
Table of Contents
The Language of Sets - This article is part of a series.
Part 2: This Article

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
#

Universal Set

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\).

Venn diagram: A ⊂ U
Venn diagram illustrating A ⊂ U: the circle of set A is contained in the rectangle of the universe U

The diagrams are named in honor of the English mathematician John Venn (1834 to 1923), who introduced them in 1880.

Venn diagram or Euler diagram?

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.

Venn diagram of three sets with the eight regions numbered and a table with the expression for each region
The 2³ = 8 regions of three sets: each is a combination of membership in A, B, and C, from region 1 (only A) to region 8 (outside all three)

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\):

Venn diagram: B ⊂ A ⊂ U
Venn diagram with three nested regions: B ⊂ A ⊂ 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
#

Definition

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.

Venn diagram of the union A ∪ B
Shaded region representing A ∪ B: all the elements that belong to A, to B, or to both

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\}$$

Venn diagram with elements: union of A and B
Example with elements distributed in the regions only A, A ∩ B, and only B

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\}$$

Venn diagram with disjoint sets
Example of a union with disjoint A and B: separate circles in the rectangle of the universe

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$$

Venn diagram: W ⊆ Z and Z ∪ W = Z
Inclusion case: W is contained in Z, so the union Z ∪ W coincides with 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
#

Definition

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.

Venn diagram of the intersection A ∩ B
Shaded region representing A ∩ B: only the elements common to A and B

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\}$$

Venn diagram with elements: intersection of A and B
Intersection with elements: only the region common to A and B is highlighted

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$$

Venn diagram with disjoint sets: empty intersection
Intersection of disjoint sets: there is no common region, so A ∩ B = ∅

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\)
Disjoint sets

\(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$$

Venn diagram: W ⊆ Z and Z ∩ W = W
Inclusion case: W is contained in Z, so the intersection Z ∩ W coincides with 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 = 2

Difference
#

Definition

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\).

Venn diagram of the difference A - B
Shaded region representing A - B: elements that are in A and not in B

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\}$$

Venn diagram with elements: A - B
Difference A - B with elements: only the elements exclusive to A remain

Venn diagram with elements: B - A
Difference B - A with elements: only the elements exclusive to B remain

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:

Venn diagram with disjoint sets: A − B is the entire circle of A
Disjoint sets: the region of A − B is the entire circle of A, so A − B = A

Venn diagram with disjoint sets: B − A is the entire circle of B
Disjoint sets: the region of B − A is the entire circle of B, so B − A = B

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$$

Venn diagram: Z - W
Difference Z - W: region of Z outside W

Venn diagram: W - Z
Difference W - Z: empty because W is contained in Z

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 orders

Combining 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
#

Definition

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\):

Venn diagram of the complement A-bar
Shaded region representing the complement of A: all the elements of U that do not belong to 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:

Visual steps of De Morgan’s first law
Step-by-step visualization of De Morgan’s first law

Visual steps of De Morgan’s second law
Step-by-step visualization of De Morgan’s second law

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:

Visual steps of distributivity: A ∩ (B ∪ C)
Step-by-step illustration of A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)

Visual steps of distributivity: A ∪ (B ∩ C)
Step-by-step illustration of A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)

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\}$$
$$A - (B \cup C) = (A - B) \cap (A - C)$$$$A - (B \cap C) = (A - B) \cup (A - C)$$

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\}\).

Symmetric difference in Exercise 2
Shaded region of A Δ B: elements exclusive to A and to B, excluding the intersection

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.

Exercise 3 (i): A ⊂ B ⊂ C
Three concentric circles representing A ⊂ B ⊂ C

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

Exercise 3 (ii): A, B, and C disjoint
Three disjoint sets in the universe U

(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\).

Exercise 3 (iii): A ⊆ B ∪ C
A contained in the region covered by B ∪ C

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

Exercise 3 (iv): A ⊆ B-bar
A disjoint from B

(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\).

Exercise 3 (v): A ⊆ B - C
A contained in the region of B 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.

Exercise 3 in strict Venn diagrams, with the empty regions hatched
The five situations of Exercise 3 as Venn diagrams: all 8 regions drawn and the necessarily empty ones hatched

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!

The Language of Sets - This article is part of a series.
Part 2: This Article

Related