---[REF]---"style":"heading"---[REF]---
---[SIDEBAR]---
"type":"objectives",
"title":"Learning Objectives",
"text":"After completing this lesson, the successful student will be able to:
- Explain why multiple SELECT queries might be combined.
- Us the UNION operator to combine multiple SELECT statements into a single result.
To combine these queries into a single SQL statement, the UNION operator would be used:
SELECT name, city
FROM museum
WHERE country = 'USA'
UNION
SELECT name, city
FROM museum
WHERE name like '%National%';
**Try the SQL statement.**
---[SQL]---
connection:SQL-Book Queries
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-usa-national-combined
---[SQL]---
SELECT name, city
FROM museum
WHERE country = 'USA'
UNION
SELECT name, city
FROM museum
WHERE name like '%National%';
---[SQL]---
There are a couple of important things to note about this combined query. First, notice the query results. The rows of the two independent queries are intermixed into one set. The museums from the United States appear along side the museums from other countries, even though the results where generated with separate SELECT statements.
Second, the structure of the combined queries is the same. In this case, both queried retrieve the name and city fields from the museum table. There are a couple of rules for the structure of queries combined with the UNION operator.
1. The combined queries must have the same number of columns.
2. The columns must be in the same order and the same or compatible data types.
---[SIDEBAR]---
"type":"note",
"title":"Columns in a Combined Query",
"text":"The columns of combined queries don't necessarily need to have the same name. If the corresponding columns of combined queries have different names, the column name in first query will be displayed at the top of the query result."
---[SIDEBAR]---
---[SIDEBAR]---
"type":"caution",
"title":"The Number of Queries Combined using Unions",
"text":"There is technically not a limit to the number of SELECT statements that can be combined using the UNION operator. Some DBMS will limit the number of queries combined using UNION. You will want to consult the documentation of the DBMS to determine constraints on the queries you wish to create."
---[SIDEBAR]---
## Including or Eliminating Duplicate Rows
Notice that the combined query in the last example omits duplicate results. The query that retrieved the museums in the United States returned 25 rows. The second query retrieved 8. You might expect that the combined query would return 33 rows, but it retrieves 32. This happens because the original queries both contained the National Gallery of Art in Washington. The combined results contain this museum only once.
By default, UNION will automatically remove duplicate rows from the query result. You can design the query to show all rows. This is done using the UNION ALL operator.
To show all rows in the previous example, you would use this syntax:
SELECT name, city
FROM museum
WHERE country = 'USA'
UNION ALL
SELECT name, city
FROM museum
WHERE name like '%National%';
**Try the SQL statement.**
---[SQL]---
connection:SQL-Book Queries
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-usa-national-combined-duplicates
---[SQL]---
SELECT name, city
FROM museum
WHERE country = 'USA'
UNION ALL
SELECT name, city
FROM museum
WHERE name like '%National%';
---[SQL]---
The results set retrieved by this SELECT statement includes 33 rows. This includes the duplicated National Gallery of Art in Washington that appears in both query results.
## Sorting Combined Query Results
You can use the ORDER BY clause to sort the combined results of UNION queries. Since a combined query can contain only one ORDER BY clause, it must appear after the final SELECT statement. For example, you may wish to sort the results of the example query used in this lesson by city in alphabetical order.
The syntax for this query is:
SELECT name, city
FROM museum
WHERE country = 'USA'
UNION
SELECT name, city
FROM museum
WHERE name like '%National%'
ORDER BY city;
**Try the SQL statement.**
---[SQL]---
connection:SQL-Book Queries
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-name-usa-national-combined-sorted
---[SQL]---
SELECT name, city
FROM museum
WHERE country = 'USA'
UNION
SELECT name, city
FROM museum
WHERE name like '%National%'
ORDER BY city;
---[SQL]---
Notice that even though the ORDER BY clause is specified in the last query, it applies to the combined results.
## Combining Queries of Different Tables
The UNION operator can also be used to combine SELECT statements that retrieve data from different tables. SELECT statements can be part of a combined query as long as the number of columns in each query is the same and the data types for each columns are compatible. Suppose you want to combine a query of the artist table with a query of the work table.
An example of such a combined query is:
SELECT full_name as name, style, 'artist' as source_table
FROM artist
WHERE style IS NOT NULL
UNION
SELECT name, style, 'work'
FROM work
WHERE style IS NOT NULL;
ORDER BY style;
**Try the SQL statement.**
---[SQL]---
connection:SQL-Book Queries
schema:canvas
diagram:canvas
owner:atlas-query
result:artist-style-work-style-combined
---[SQL]---
SELECT full_name as name, style, 'artist' as source_table
FROM artist
WHERE style IS NOT NULL
UNION
SELECT name, style, 'work'
FROM work
WHERE style IS NOT NULL
ORDER BY style;
---[SQL]---
This example demonstates a few aspects of combined queries. First, notice that aliases are used to rename columns in the first query. The aliases are used as the column headings for the combined results.
Second, both queries include a WHERE clause. Combined queries can use any of the clauses found in a single SELECT statement. Two notable exceptions to this are that only the last query in a combined query can include an ORDER BY clause and only the column names specified in the first query will be used in the combined results.
Third, the queries retrieve rows from different tables. This works because the queries have the same number of columns and the relative columns in each of the tables have compatible data types. The first column each query has the name of the artist or work, respectively. The second column has data about the style of the artist or work. The last column contains either the text 'artist' or 'work'.
---[SIDEBAR]---
"type":"tip",
"title":"Label Values in Combined Queries",
"text":"The use of the words 'artist' and 'work' in the example demonstrate how using literals as labels can be helpful with combined queries. In the first query, the word 'artist' is included for each row. In the second, each row will contain the word 'work'. When the two queries are combined, it is easy to determine which of the rows in the combined result come from the artist table and which come from the work table."
---[SIDEBAR]---
## Summary
This lesson described how to combine multiple SELECT statements using the UNION operator. Queries can be combined either to show rows that are duplicated in the original queries or to show only the unique set of results. The original queries can contain any of the clauses and SQL features discussed elsewhere in this resource. The combined results can also be combined using an ORDER BY clause in the last combined query.