12.8. SQL: Union and Simple Subqueries

This section discusses two ways in which select queries may be used as components within a larger select query. First we examine how to specify the operations of union, intersection, and difference in SQL. Next we look at basic subqueries in SQL. Let’s begin with the union operation. The basic syntax is shown here. The reserved word “union” is allowed only between select queries, not between table names.


union [all]



The select queries must be union compatible (i.e., same number of columns, and corresponding columns are based on compatible data types). By default, a set is returned which is the union of the tables returned from the individual select queries, with any duplicate ...

Get Information Modeling and Relational Databases, 2nd 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.