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 scenarios. ...

Get The Language of SQL, 3rd 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.