SQL Joins   «Prev  Next»

Lesson 8UNION
ObjectiveCreate an SQL statement that will create the join specified.

How to perform UNION in SQL?

The UNION operator lets you combine the results of SELECT statements from multiple tables. For example, if you have a table that contains store location information, you may have more than one of these tables, one for each location. You can use the UNION join to get a single listing that shows the store information from all locations in one results set:

Creating a UNION statement in SQL to combine data from the "Publishers Table" and the "Titles Table" requires a common data structure from both tables, as UNION combines the results of two or more SELECT statements into a single result set. However, it's important to note that the "Publishers Table" and the "Titles Table" have different columns and data types, making a direct UNION operation between these tables conceptually inappropriate and technically unfeasible without modification.
If the intention is to demonstrate the use of UNION with these tables, one must first ensure the SELECT statements for both tables produce a result set with the same number of columns, having compatible data types in each column. This often involves selecting a subset of columns that are common in structure or type, or using placeholder values where direct matches don't exist. Given the structure of your tables, here's an illustrative example:
  1. Publishers Table** (pub_id, pub_name, city, state, country)
  2. Titles Table** (title_id, pub_id, price, pubdate)
A possible UNION query could be:
-- Selecting pub_id and a second column from each table
-- For Publishers, pub_name is selected
-- For Titles, title_id is converted to a varchar (assuming it's numeric) to match the type of pub_name
-- Placeholder columns (NULLs) are used to match the column count in both SELECT statements

SELECT pub_id, pub_name AS second_column, NULL AS third_column, NULL AS fourth_column, NULL AS fifth_column
FROM Publishers
UNION
SELECT pub_id, CAST(title_id AS VARCHAR) AS second_column, NULL, NULL, NULL
FROM Titles;

In this query:
  • The first SELECT statement takes `pub_id` and `pub_name` from the Publishers Table, along with three NULL columns as placeholders.
  • The second SELECT statement takes `pub_id` from the Titles Table, converts `title_id` to a VARCHAR to match the data type of `pub_name`, and includes three NULL columns.
  • The UNION operator combines the results of these two SELECT statements into a single result set.
This example is primarily for educational purposes to demonstrate the use of UNION. In practical scenarios, UNION is more commonly used with tables or datasets that have a more directly comparable structure and purpose.


SELECT * FROM Store1 
UNION SELECT * FROM Store2

The UNION operator will automatically remove duplicate information for you. It can be helpful if you find yourself working with distributed tables wanting to get a comprehensive view of what is in them.
We have covered a lot of material about joins in the last several lessons. Complete this exercise to see if you can do a more advanced join.

Advanced Join - Exercise