Part 3 of 5 · 1 min read
Union, set difference, and Cartesian product
Combining two relations, or pairing every row of one with every row of another.
Think of two guest lists for two parties. Union is everyone invited to either party. Set difference is everyone on the first list but not the second. A Cartesian product is every possible pairing of one guest from each list.
In plain words: union (∪) combines the rows of two relations with the same columns, keeping every row that appears in either one. Set difference (−) keeps rows that appear in the first relation but not the second. Both require the two relations to share the same columns. Cartesian product (×) pairs every row of one relation with every row of another, with no condition at all, producing far more rows than either input alone.
A worked example. Union: every customer who placed an order in January or February. Cartesian product: pairing every customer with every book, before any filtering narrows the pairs down to anything meaningful.
The key lines.
customers_jan ∪ customers_feb
customers × books
Union, set difference, Cartesian product
Union (∪) keeps every row in either of two same-shaped relations. Set difference (−) keeps rows in the first relation but not the second. Cartesian product (×) pairs every row of one relation with every row of another.
Example: customers × books, with 400 customers and 2,000 books, produces 800,000 paired rows, almost all of them meaningless without a further selection.