Combining Rows with UNION
In the next three sections, I’ll cover the UNION, INTERSECT, and EXCEPT set operators, which combine the results of two SELECT statements into a single result. These operators can be mixed and chained to combine more than two tables. The UNION operator is supported widely; the other two operators, less so.
A UNION operation combines the results of two queries into a single result that comprises the rows returned by both queries. (This operation differs from a join, which combines columns from two tables.) A UNION expression removes duplicate rows from the result; a UNION ALL expression doesn’t remove duplicates.
Unions are simple, but they have some restrictions:
The SELECT-clause lists in the two queries must have ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access