Chapter 16

Set Operations

Abstract

Since SQL is based on sets, it includes classic set operations, with extensions for multisets.

Keywords

ALL

DISTINCT

EXCEPT

EXCEPT ALL

INTERSECT

INTERSECT ALL

Multisets (bags)

Proper subsets

Redundant duplicates

Set equality

UNION

UNION ALL

Union compatible

By set operations, I mean union, intersection, and set differences where the sets in SQL are tables. These are the basic operators used in elementary set theory, which has been taught in the United States public school systems for decades. Since the relational model is based on sets, you would expect that SQL would have had a good variety of set operators. But there is a problem in SQL that you did not have in high school set theory. SQL tables are multisets ...

Get Joe Celko's SQL for Smarties, 5th 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.