Chapter 9. Set Operations

Recall from Chapter 2 that set theory is fundamental to the relational model. But whereas mathematical sets are unchanging, d.atabase sets are dynamic—they grow, shrink, and otherwise change over time. This chapter covers the following SQL set operators, which combine the results of two SELECT statements into one result:

  • UNION returns all the rows returned by both queries, with duplicates removed.

  • INTERSECT returns all rows common to both queries (that is, all distinct rows retrieved by both queries).

  • EXCEPT returns all rows from the first query without the rows that appear in the second query, with duplicates removed.

These set operations aren't joins, but you can mix and chain them to combine two or more tables.

This chapter ...

Get SQL: Visual Quickstart Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.