FULL OUTER JOIN vs CROSS JOIN

As I have been working on the SQL Server JOIN Types poster, I have received several questions around the difference between a CROSS JOIN, and a FULL OUTER JOIN. After looking at the Venn diagrams for the two, they are both shown as the same, however they are not the same by any means.

Yes, they both include all rows from both the LEFT and RIGHT side of the JOIN, however they are matched up or JOINed in a very different way.

Lets take a look, first at the Venn diagrams, you can see below that the Venn diagrams show that all rows from Table1 are included in the results, and all rows from Table2 are included in the results. This is indeed correct, the differences come in the number of rows and how the results are matched up.

CrossJoinVennFullOuterJoinVenn

 

Now lets take a look at some sample cod that shows the differences. First we will create a database and create a couple tables to use for the demo, and each table has 3 rows inserted to it:

CREATE DATABASE [QueryTraining];
GO
USE [QueryTraining];
GO

CREATE TABLE People
(
	id   INTEGER IDENTITY NOT NULL,
	Name VARCHAR(100) NOT NULL,
    fk   INTEGER NULL
PRIMARY KEY (id)
) ;

CREATE TABLE Colors
(
	id   INTEGER NOT NULL,
	FavoriteColor VARCHAR(100) NOT NULL
) ;

INSERT INTO Colors (id, FavoriteColor) VALUES
    (1, 'red'),
    (2, 'green'),
    (3, 'blue');

INSERT INTO People (Name, fk) VALUES
    ('Steve', 1),
    ('Aaron', 3),
    ('Mary', NULL);

Look at this as a database to store people and their favorite color, in this example, there are 3 people , and 3 colors. One person does not have a favorite color, and one color has not been favorited by anyone.

Now lets take a look at the differences between the two.

CROSS JOIN

CrossJoinVenn

The CROSS JOIN gives you the Cartesian product of the two tables, by matching every row from one table with every row from another table. If there are X rows in the first table, and Y rows in the second table, the result set will have exactly X times Y rows. Notice on the CROSS JOIN, there is no ON clause specified. This is because there is not matching.

CROSS JOIN Sample

SELECT *
  FROM People p
  CROSS JOIN Colors c;

Which gives us the following result set:

CrossJoinResults

 

In the above result set, you can see that for each row in the People table (3) it was matched up with each row  from the Colors table (3) which gives us a total of 3 x 3 = 9 rows in the result set.

FULL OUTER JOIN

Now for the full outer JOIN, this is like asking for a LEFT OUTER JOIN and a RIGHT OUTER JOIN in the same query. You are asking to get every row from Table1 matching what could be matched in Table2, for those that don’t match on the Table1 side, just display the Table2 side, with NULLs on the LEFT side, and for those that don’t match on the TABLE2 side, just display the Table1 side with NULLs on the RIGHT side.

FullOuterJoinVenn
FULL OUTER JOIN Sample

SELECT *
  FROM People p
  FULL OUTER JOIN Colors c
    ON p.fk = c.id;

In this case everything from Table1 will be represented, and everything from Table2 will be shown, but they will be matched up base on the ON clause, rather than with the CROSS JOIN which gives every possible combination.FullOuterJoinResults

I hope that this helps answer some of the questions with the differences between a CROSS JOIN and a FULL OUTER JOIN.

If you haven’t already taken a look, please check out the free download of the SQL Server JOIN Types poster.

For More Information:

Brown Bag Lunch: LinkedIn Pro Tips & How to Build Your Personal Brand

This week I had a chance to sit in on a great presentation provided by Sydney Ratzlaff at Emergency Reporting.

I learned a few things, and was able to build out my LinkedIn profile based on her help.

Here are the slides from the presentation on SlideShare:

 

See Also:

Training Employees

Training Employees

A great conversation, not original, but something found on LinkedIn

For employee training, see our Free SQL Query Training program.

 

Whiteboard Interview Questions

Last week Aaron Buma and I were attended a session at Western Washington University where we were asked to present to the ACM club on how to work through the white board coding questions in the technical interview.

 

Agile Terminology – Sunday Fun Crossword – Answer Key

Please click the read more link to get the answers to the Sunday fun Agile crossword puzzle.

Read more of this post

IT Guy – 3.0

Great video that I discovered on YouTube today. 

Some great lines in this video…

“The project gets crazy and they call me to save it, so I search Pinal Dave and copy and paste it”.

“Your company is so backwards they still use Lotus Notes”.