Click here to Skip to main content
15,886,422 members
Articles / Productivity Apps and Services / Sharepoint

What Is the Difference Between a Join and a UNION?

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
1 Jan 2016MIT4 min read 55.8K   3  
Difference between a join and a union

Both Joins and UNIONS can be used to combine data from two or more tables. Read this article to find out each command’s strengths and when to use them.

To get the most of this and our other lessons, be sure to practice using the examples!

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database. You can get started using these free tools using my Guide Getting Started Using SQL Server.

What Is the Difference Between a Join and UNION?

Joins and Unions can be used to combine data from one or more tables. The difference lies in how the data is combined.

In simple terms, joins combine data into new columns. If two tables are joined together, then the data from the first table is shown in one set of column alongside the second table’s column in the same row.

Unions combine data into new rows. If two tables are “unioned” together, then the data from the first table is in one set of rows, and the data from the second table in another set. The rows are in the same result.

Here is a visual depiction of a join. Table A and B’s columns are combined into a single result.

Use Joins to Combine Columns

Joins Combine Columns

Each row in the result contains columns from BOTH table A and B. Rows are created when columns from one table match columns from another. This match is called the join condition.

This makes joins really great for looking up values and including them in results. This is usually the result of denormalizing (reversing normalization) and involves using the foreign key in one table to look up column values by using the primary key in another.

Now compare the above depiction with that of a union. In a union, each row within the result is from one table OR the other. In a union, columns aren’t combined to create results, rows are combined.

Use a union to combine rows

Unions Combine Rows

Unions are typically used where you have two results whose rows you want to include in the same result. A use case may be that you have two tables: Teachers and Students. You would like to create a master list of name and birthdays sorted by date.

To do this, you can use a union to first combine the rows into a single result and then sort them.

Let’s now take a slightly deeper look into both.

Combining Data with a Join

In this section, we'll look at the inner join. It is one of the most common forms of join and is used when you need to match rows from two tables. Rows that match remain in the result, those that don’t are rejected.

Below is an example of a simple select statement with an INNER JOIN clause.

SQL
SELECT columnlist
FROM   maintable
       INNER JOIN 
       secondtable ON join condition

Here is an example of using a join to lookup an employee’s name:

SQL
SELECT   <span style="color: rgb(0, 0, 255);">Employee.NationalIDNumber,
         Person.FirstName,
         Person.LastName,
         Employee.JobTitle</span>
FROM     HumanResources.Employee
         INNER JOIN
         Person.Person
         ON <span style="color: rgb(255, 0, 0);">HumanResources.Employee.BusinessEntityID = person.BusinessEntityID</span>
ORDER BY person.LastName;

Here are the results:

Results of an Inner Join

INNER JOIN Results

You can learn more about INNER JOINS here, but for now, here are two things I want to point out.

First, notice the join condition, see how we are matching BusinessEntityID from both tables.

Second, check out that the results contain columns from both tables.

Combining Data with a UNION

Let’s take a closer look at the UNION statement. In SQL, the UNION statement looks like:

SQL
SELECT columnlist
FROM   table1
UNION
SELECT columnlist
FROM   table2

Suppose you were asked to provide a list of all AdventureWorks2012 product categories and subcategories. To do this, you could write two separate queries and provide two separate results, such as two spreadsheets, or you could use the UNION clause to deliver one result.

SQL
SELECT C.Name
FROM   Production.ProductCategory AS C
UNION ALL
SELECT S.Name
FROM   Production.ProductSubcategory AS S

In order to union two tables, there are a couple of requirements:

  1. The number of columns must be the same for both select statements.
  2. The columns, in order, must be of the same data type.

When rows are combined, duplicate rows are eliminated. If you want to keep all rows from both select statements' results, use the ALL keyword.

Conclusion

Both joins and unions can be used to combine data from one or more tables into a single result. They both go about this in different ways. Whereas a join is used to combine columns from different tables, the union is used to combine rows.

The post What Is the Difference Between a Join and a UNION? appeared first on Essential SQL.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --