UNION

Although UNION isn't a join, it is a way of combining data from multiple tables into one display. UNION is useful when you want to see similar data from two or more tables in one display. The simplified syntax is this:

select_statement
UNION
select_statement

Following is a query that will give you a list of all the authors and editors who live in Oakland or Berkeley:

SQL
select au_fname, au_lname, city
from authors
where city in ('Oakland', 'Berkeley')
union
select ed_fname, ed_lname, city
from editors
where city in ('Oakland', 'Berkeley')

Notice that the two SELECT lists contain the same number of items and the datatypes are compatible. Check your documentation for specifics on what datatype combinations you can use.

When you examine ...

Get Practical SQL Handbook, The: Using SQL Variants, Fourth 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.