71

What is the difference between a FULL JOIN and an INNER JOIN?

When I do a FULL JOIN, I get 832 records and with an INNER JOIN, I get 830 records.

8 Answers 8

151

NOTE: All of these can be found on Wikipedia: Join (SQL).

There are three types of OUTER joins:

  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • FULL OUTER JOIN

The keyword OUTER is optional in all implementations that follow the standard, so FULL JOIN is the same as FULL OUTER JOIN. (I've omitted the word OUTER from the SQL in the rest of this answer.)

Let's look at what each does.

Consider the following two input data sets:

 Set "A"    Set "B"

 AA         BB
--------   --------
 Item 1     Item 3
 Item 2     Item 4
 Item 3     Item 5
 Item 4     Item 6

Notice that there are some items in A that aren't in B, and vice versa.

Now, if we write an SQL statement like this, using LEFT join:

SELECT * FROM A LEFT JOIN B ON AA = BB

You'll get the following result (the empty holes are actually NULL marks):

 AA         BB
--------   --------
 Item 1
 Item 2
 Item 3     Item 3
 Item 4     Item 4

Notice that you'll get all the rows from AA, or rather, all the rows from the left part of the join clause.

If you switch to using a RIGHT join:

SELECT * FROM A RIGHT JOIN B ON AA = BB

 AA         BB
--------   --------
 Item 3     Item 3
 Item 4     Item 4
            Item 5
            Item 6

Notice that you get all the rows from the right part of the join clause.

However, if you want all the rows of both, you'll use a FULL join:

SELECT * FROM A FULL JOIN B ON AA = BB

 AA         BB
--------   --------
 Item 1            <-----+
 Item 2                  |
 Item 3     Item 3       |
 Item 4     Item 4       |
            Item 5       +--- empty holes are NULL's
            Item 6       |
   ^                     |
   |                     |
   +---------------------+

As suggested in a comment, let me complete the other different ways to join.

With INNER join:

SELECT * FROM A INNER JOIN B ON AA = BB

 AA         BB
--------   --------
 Item 3     Item 3
 Item 4     Item 4

With INNER join we only get the rows that actually match up, no holes because of joining.

A CROSS join produces a cartesian product, by matching up every row from the first set with every row from the second set:

SELECT * FROM A CROSS JOIN B

 AA         BB
--------   --------
 Item 1     Item 3      ^
 Item 1     Item 4      +--- first item from A, repeated for all items of B
 Item 1     Item 5      |
 Item 1     Item 6      v
 Item 2     Item 3      ^
 Item 2     Item 4      +--- second item from A, repeated for all items of B
 Item 2     Item 5      |
 Item 2     Item 6      v
 Item 3     Item 3      ... and so on
 Item 3     Item 4
 Item 3     Item 5
 Item 3     Item 6
 Item 4     Item 3
 Item 4     Item 4
 Item 4     Item 5
 Item 4     Item 6

Also note that we don't specify which columns that match, since there is no matching done.

Finally, NATURAL join, in this syntax we don't specify which columns that match, but matches on column names. In our contrived example, no column names are the same, but let's say for this specific example that the column names was XX in both tables, then we would get the following result:

SELECT * FROM A NATURAL JOIN B

 +----------+------- matches on the names, and then the data
 |          |
 v          v
 XX         XX
--------   --------
 Item 3     Item 3
 Item 4     Item 4

As you can see, you get the same as a INNER join, but don't have to type out the match part of the join clause.

5
  • @polygenelubricants - yes, but that is not a type of OUTER join.
    – tster
    Commented Jun 11, 2010 at 12:52
  • 1
    Really great explanation. Can you please explain "CROSS APPLY" in sql server in the same way?
    – hemanth
    Commented Feb 28, 2014 at 4:56
  • I think the APPLY operators need their own question. Commented Feb 28, 2014 at 6:12
  • Very good answer. Could please you also provide the examples and the respective SQL queries for the remaining situations (i.e. where NULLs of a left, right or full join are removed) of the answer of Ralf de Kleine?
    – nbro
    Commented Mar 21, 2016 at 23:03
  • You got my upvote for the ASCII art (and the good explanation) :)
    – SebastianH
    Commented Apr 19, 2021 at 10:23
86

A FULL OUTER JOIN is a union of the LEFT OUTER JOIN and RIGHT OUTER JOIN.

(did that make sense?)

Nice visual explanation describing joins (bottom left describes full outer join): http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

Credits go to C.L. Moffatt's blogpost on codeproject

5
  • 7
    Cite your source? looks like codeproject.com/Articles/33052/… Commented Jun 18, 2014 at 2:39
  • It's been a while, it could be the codeproject you references. Commented Jun 19, 2014 at 11:39
  • Ok, I've checked it, it is M.L Moffat's blogpost where I must have found it. Commented Aug 20, 2015 at 14:40
  • 1
    Which do you get if you only use the keyword JOIN? Commented Jun 16, 2021 at 20:16
  • @AaronFranke that would be resolved to inner join most likely. Commented Jun 21, 2021 at 15:03
6

In a pretty simple way, the main difference is:

INNER JOIN - Returns only matched rows. Therefore, unmatched rows are not included.

FULL JOIN - Returns those rows that exist in the right table and not in the left, plus the rows that exist in the left table and not in the right, beyond the inner join rows.

4

The difference is in the behaviour of unmatched rows.

For example, if table A has a row which doesn't have a correspondence in table B in the field onto which the join is defined, an inner join would omit the row altogether, while a full join would include the row, but with NULL values for the fields of table B. Viceversa for unmatched rows of table B.

2

A full join will return rows from both tables even if there are no matching rows in the other table. A full join is like a right join and a left join at the same time. An inner join will only return rows which have at least 1 partner in the other table.

0

Inner join wouldn't bring any NULLs in the join target field. If there's no matching record, the original will not be in the table.

1
  • 1
    when there's no match, outer join returns nulls Commented Jun 11, 2010 at 12:53
0

That means that your tables matches quite well.

The Wiki page found here shows a quite a good example of how they work.

0

Consider Table A and Table B

Table A - (Key, Name)

1, Data1 2, Data2 4, Data4 5, Data5

Table B - (Key, FKey, Name)

1, 1, DataA 2, 2, DataB 3, NULL, DataC 4, 4, DataD

An inner join would return

  • 1, Data1, 1, 1, DataA
  • 2, Data2, 2, 2, DataB
  • 4, Data3, 4, 4, DataD

whilst a full outer join would return

  • 1, Data1, 1, 1, DataA
  • 2, Data2, 2, 2, DataB
  • NULL, NULL, 3, NULL, DataC
  • 4, Data4, 4, 4, DataD
  • 5, Data5, NULL, NULL, NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.