15. Set Logic

Keywords Introduced

UNION  •  UNION ALL  •  INTERSECT  •  EXCEPT

The various joins and subqueries of the previous few chapters have dealt with different ways of combining data from multiple tables. The result, however, has always been a single SELECT statement. Now we’ll extend the concept of combining data in multiple tables to the possibility of combining data from multiple queries. In other words, we’ll look at a way to write a single SQL statement that contains more than one SELECT to retrieve data.

The concept of combining queries is often referred to as set logic, a term taken from mathematics. Each SELECT query can be referred to as a set of data. The set logic we’ll employ and examine in this chapter will address four basic ...

Get The Language of SQL, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.