Uses for UNION

You probably won’t use UNION as much as INNER JOIN and OUTER JOIN. You most likely will use UNION to combine two or more similar result sets from different tables. Although you can use UNION to combine two result sets from the same table or set of tables, you usually can solve those sorts of problems with a simple SELECT statement containing a more complex WHERE clause. We include a couple of examples in the Sample Statements section and show you the more efficient way to solve the same problem with a WHERE clause instead of a UNION.

Here’s just a small sample of the types of problems you can solve with UNION using the sample databases.

“Show me all the customer and employee names and addresses.”

“List all the customers who ordered ...

Get SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL, Second 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.