Set Operations

Learn how to combine, compare, and differentiate query results using set operations.

We'll cover the following...

Imagine that our online store wants to run a special promotion.

The marketing team has two lists: one containing customers who have purchased electronics and another containing customers who have purchased books. They want to create a single combined list to send an email campaign, but they must ensure that no individual receives the message twice if they appear on both lists.

How can they efficiently combine these datasets while eliminating duplicates?

This scenario illustrates how set operations in relational algebra can be applied effectively. These operations are essential tools that allow us to combine, compare, and filter the results of multiple queries in a structured and efficient manner.

By the end of this lesson, we’ll be able to:

  • Combine results from multiple queries using UNION and UNION ALL.

  • Find the common records between the two queries using INTERSECT.

  • Discover records that exist in one query but not another using EXCEPT.

  • Understand the foundational Cartesian Product operation.

Let’s dive in and see how we can manipulate our datasets!

The UNION operator

Sometimes, we need to combine the results of two or more separate queries into a single result set.

This process is particularly useful when we want to obtain a consolidated list from related subsets of data. To achieve this, we use the UNION operator. The ...