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

Get SQL: Visual QuickStart Guide 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.