2051

What is the difference between LEFT JOIN and LEFT OUTER JOIN?

Quiver
  • 1,351
  • 6
  • 33
  • 56
KG Sosa
  • 21,565
  • 6
  • 26
  • 27

13 Answers13

2728

As per the documentation: FROM (Transact-SQL):

<join_type> ::= 
    [ { INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } } [ <join_hint> ] ]
    JOIN

The keyword OUTER is marked as optional (enclosed in square brackets). In this specific case, whether you specify OUTER or not makes no difference. Note that while the other elements of the join clause is also marked as optional, leaving them out will make a difference.

For instance, the entire type-part of the JOIN clause is optional, in which case the default is INNER if you just specify JOIN. In other words, this is legal:

SELECT *
FROM A JOIN B ON A.X = B.Y

Here's a list of equivalent syntaxes:

A LEFT JOIN B            A LEFT OUTER JOIN B
A RIGHT JOIN B           A RIGHT OUTER JOIN B
A FULL JOIN B            A FULL OUTER JOIN B
A INNER JOIN B           A JOIN B

Also take a look at the answer I left on this other SO question: SQL left join vs multiple tables on FROM line?.

philipxy
  • 14,867
  • 6
  • 39
  • 83
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • 166
    Absolutely correct. OUTER is allowed for ANSI-92 compatibility. – Sean Reilly Jan 02 '09 at 21:34
  • 24
    @LasseV.Karlsen wouldnt it be better to have `INNER JOIN` on the right and just `JOIN` on the left in the list of equivalents? – nawfal May 01 '13 at 14:55
  • 12
    @LasseV.Karlsen I just meant that the left side has the concise form and the right side has the expanded form. I thought it would make it coherent if you followed the same for `JOIN`s as well. – nawfal May 02 '13 at 07:40
  • 1
    According to Standard SQL `OUTER` is also an optional keyword: ** ::= INNER | [ OUTER ]** – dnoeth Oct 12 '15 at 15:56
865

To answer your question there is no difference between LEFT JOIN and LEFT OUTER JOIN, they are exactly same that said...

At the top level there are mainly 3 types of joins:

  1. INNER
  2. OUTER
  3. CROSS

  1. INNER JOIN - fetches data if present in both the tables.

  2. OUTER JOIN are of 3 types:

    1. LEFT OUTER JOIN - fetches data if present in the left table.
    2. RIGHT OUTER JOIN - fetches data if present in the right table.
    3. FULL OUTER JOIN - fetches data if present in either of the two tables.
  3. CROSS JOIN, as the name suggests, does [n X m] that joins everything to everything.
    Similar to scenario where we simply lists the tables for joining (in the FROM clause of the SELECT statement), using commas to separate them.


Points to be noted:

  • If you just mention JOIN then by default it is a INNER JOIN.
  • An OUTER join has to be LEFT | RIGHT | FULL you can not simply say OUTER JOIN.
  • You can drop OUTER keyword and just say LEFT JOIN or RIGHT JOIN or FULL JOIN.

For those who want to visualise these in a better way, please go to this link: A Visual Explanation of SQL Joins

daveloyall
  • 2,140
  • 21
  • 23
sactiw
  • 21,935
  • 4
  • 41
  • 28
  • 23
    Very good answer. It will be clearer if you say "LEFT OUTER JOIN - fetches all data from the left table with matching data from right, if preset." for 2.1 (and similar change for 2.2) – ssh Dec 27 '12 at 19:27
  • 3
    Also you can do cross join by simply 'select * from TableA,TableB' – om471987 Feb 10 '13 at 18:22
  • 4
    Sorry if I'm necrobumping, but is `CROSS JOIN` the same as `FULL JOIN`? – TechnicalTophat Jul 13 '16 at 13:11
  • 17
    @RhysO no, CROSS JOIN is a Cartesian product i.e. CROSS JOIN of a table, having n rows, with a table, having m rows, will always give (n*m) rows while FULL OUTER JOIN of a table, having n rows, with a table, having m rows, will give at max (n+m) rows – sactiw Jul 13 '16 at 15:34
  • 2
    @sactiw consider to add a special note in your valuable answer about the difference between `Cross Join` and `Full Outer Join` ... in some way seems similar. – Manuel Jordan Oct 12 '19 at 14:51
  • 1
    @ManuelJordan They only "seem similar" because the descriptions here are so vague & poor. But that should be clear if you take the descriptions here and try to use them to calculate example results of operator calls ... you can't, too vague & poor. Anyway everything after the first sentence is irrelevant to the question post. Also the blog at the link at the end is a mess & the author admits the blog is confused & not useful in their own comments on their blog. [Venn diagrams are not useful for describing joins.](https://stackoverflow.com/a/55642928/3404097) – philipxy Feb 03 '23 at 10:49
447

What is the difference between left join and left outer join?

Nothing. LEFT JOIN and LEFT OUTER JOIN are equivalent.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
94

Left Join and Left Outer Join are one and the same. The former is the shorthand for the latter. The same can be said about the Right Join and Right Outer Join relationship. The demonstration will illustrate the equality. Working examples of each query have been provided via SQL Fiddle. This tool will allow for hands on manipulation of the query.

Given

enter image description here

Left Join and Left Outer Join

enter image description here

Results

enter image description here


Right Join and Right Outer Join

enter image description here

Results

enter image description here

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
78

I'm a PostgreSQL DBA, as far as I could understand the difference between outer or not outer joins difference is a topic that has considerable discussion all around the internet. Until today I never saw a difference between those two; So I went further and I try to find the difference between those. At the end I read the whole documentation about it and I found the answer for this,

So if you look on documentation (at least in PostgreSQL) you can find this phrase:

"The words INNER and OUTER are optional in all forms. INNER is the default; LEFT, RIGHT, and FULL imply an outer join."

In another words,

LEFT JOIN and LEFT OUTER JOIN ARE THE SAME

RIGHT JOIN and RIGHT OUTER JOIN ARE THE SAME

I hope it can be a contribute for those who are still trying to find the answer.

shA.t
  • 16,580
  • 5
  • 54
  • 111
andrefsp
  • 797
  • 5
  • 2
57

I find it easier to think of Joins in the following order:

  • CROSS JOIN - a Cartesian product of both tables. ALL joins begin here
  • INNER JOIN - a CROSS JOIN with a filter added.
  • OUTER JOIN - an INNER JOIN with missing elements (from either LEFT or RIGHT table) added afterward.

Until I figured out this (relatively) simple model, JOINS were always a bit more of a black art. Now they make perfect sense.

Hope this helps more than it confuses.

frozenjim
  • 746
  • 5
  • 11
48

To answer your question

In Sql Server joins syntax OUTER is optional

It is mentioned in msdn article : https://msdn.microsoft.com/en-us/library/ms177634(v=sql.130).aspx

So following list shows join equivalent syntaxes with and without OUTER

LEFT OUTER JOIN => LEFT JOIN
RIGHT OUTER JOIN => RIGHT JOIN
FULL OUTER JOIN => FULL JOIN

Other equivalent syntaxes

INNER JOIN => JOIN
CROSS JOIN => ,

Strongly Recommend Dotnet Mob Artice : Joins in Sql Server enter image description here

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
mass
  • 612
  • 5
  • 7
42

Why are LEFT/RIGHT and LEFT OUTER/RIGHT OUTER the same? Let's explain why this vocabulary. Understand that LEFT and RIGHT joins are specific cases of the OUTER join, and therefore couldn't be anything else than OUTER LEFT/OUTER RIGHT. The OUTER join is also called FULL OUTER as opposed to LEFT and RIGHT joins that are PARTIAL results of the OUTER join. Indeed:

Table A | Table B     Table A | Table B      Table A | Table B      Table A | Table B
   1    |   5            1    |   1             1    |   1             1    |   1
   2    |   1            2    |   2             2    |   2             2    |   2
   3    |   6            3    |  null           3    |  null           -    |   -
   4    |   2            4    |  null           4    |  null           -    |   -
                        null  |   5             -    |   -            null  |   5
                        null  |   6             -    |   -            null  |   6

                      OUTER JOIN (FULL)     LEFT OUTER (partial)   RIGHT OUTER (partial)

It is now clear why those operations have aliases, as well as it is clear only 3 cases exist: INNER, OUTER, CROSS. With two sub-cases for the OUTER. The vocabulary, the way teachers explain this, as well as some answers above, often make it looks like there are lots of different types of join. But it's actually very simple.

Yugo Amaryl
  • 1,249
  • 2
  • 15
  • 21
  • 2
    "it is clear only 3 cases exist": interesting but flawed. Consider that an inner join is a specialised cross join (i.e. move join predicates to the where clause). Further consider that outer join isn't a join at all, rather is a union where are used nulls in place of 'missing' columns. Therefore, it could be argued that cross is the only join required. Note the current thinking in relational theory is that natural join satisfies all join requirements. Aside: can you explain if/how the vocabulary "`JOIN` implies `INNER JOIN`" fits with your reasoning for outer join vocab? – onedaywhen Jul 06 '16 at 10:39
32

There are only 3 joins:

  • A) Cross Join = Cartesian (E.g: Table A, Table B)

  • B) Inner Join = JOIN (E.g: Table A Join/Inner Join Table B)

  • C) Outer join:

    There are three type of outer join

    1. Left Outer Join = Left Join
    2. Right Outer Join = Right Join
    3. Full Outer Join = Full Join
TylerH
  • 20,799
  • 66
  • 75
  • 101
Delickate
  • 1,102
  • 1
  • 11
  • 17
23

There are mainly three types of JOIN

  1. Inner: fetches data, that are present in both tables
    • Only JOIN means INNER JOIN
  2. Outer: are of three types

    • LEFT OUTER - - fetches data present only in left table & matching condition
    • RIGHT OUTER - - fetches data present only in right table & matching condition
    • FULL OUTER - - fetches data present any or both table
    • (LEFT or RIGHT or FULL) OUTER JOIN can be written w/o writing "OUTER"
  3. Cross Join: joins everything to everything

sjngm
  • 12,423
  • 14
  • 84
  • 114
Harsh
  • 247
  • 2
  • 3
20

Syntactic sugar, makes it more obvious to the casual reader that the join isn't an inner one.

Uncle Iroh
  • 5,748
  • 6
  • 48
  • 61
Unsliced
  • 10,404
  • 8
  • 51
  • 81
  • 2
    So... what's a FULL OUTER JOIN then? – Amy B Jan 02 '09 at 20:29
  • 6
    tableA FULL OUTER JOIN tableB will give you three types of records: all records in tableA with no matching record in tableB, all records in tableB with no matching record in tableA, and all records in tableA with a matching record in tableB. – Dave DuPlantis Oct 05 '09 at 18:16
18

Just in the context of this question, I want to post the 2 'APPLY' operators as well:

JOINS:

  1. INNER JOIN = JOIN

  2. OUTER JOIN

    • LEFT OUTER JOIN = LEFT JOIN

    • RIGHT OUTER JOIN = RIGHT JOIN

    • FULL OUTER JOIN = FULL JOIN

  3. CROSS JOIN

SELF-JOIN: This is not exactly a separate type of join. This is basically joining a table to itself using one of the above joins. But I felt it is worth mentioning in the context JOIN discussions as you will hear this term from many in the SQL Developer community.

APPLY:

  1. CROSS APPLY -- Similar to INNER JOIN (But has added advantage of being able to compute something in the Right table for each row of the Left table and would return only the matching rows)
  2. OUTER APPLY -- Similar to LEFT OUTER JOIN (But has added advantage of being able to compute something in the Right table for each row of the Left table and would return all the rows from the Left table irrespective of a match on the Right table)

https://www.mssqltips.com/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/

https://sqlhints.com/2016/10/23/outer-apply-in-sql-server/

Real life example, when to use OUTER / CROSS APPLY in SQL

I find APPLY operator very beneficial as they give better performance than having to do the same computation in a subquery. They are also replacement of many Analytical functions in older versions of SQL Server. That is why I believe that after being comfortable with JOINS, one SQL developer should try to learn the APPLY operators next.

san
  • 1,415
  • 8
  • 13
-1

In SQL, the terms "LEFT JOIN" and "LEFT OUTER JOIN" are often used interchangeably. Both of these join types return all the rows from the left table (the table specified before the JOIN keyword) and the matching rows from the right table (the table specified after the JOIN keyword). However, there is a slight difference in the way they handle unmatched rows.

A LEFT JOIN or LEFT OUTER JOIN includes all the rows from the left table, regardless of whether there is a match in the right table. If there is no match, the result will contain NULL values in the columns of the right table.

Here's an example to illustrate the difference:

Let's assume we have two tables, Customers and Orders, with a common column CustomerID:

Customers Table:

CustomerID | CustomerName
-----------|--------------
1          | John
2          | Mary
3          | David

Orders Table:

OrderID | CustomerID | OrderDate
--------|------------|-----------
101     | 1          | 2023-01-01
102     | 2          | 2023-02-01

Using a LEFT JOIN or LEFT OUTER JOIN between these tables, the result would be:

SELECT *
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:

CustomerID | CustomerName | OrderID | CustomerID | OrderDate
-----------|--------------|---------|------------|-----------
1          | John         | 101     | 1          | 2023-01-01
2          | Mary         | 102     | 2          | 2023-02-01
3          | David        | NULL    | NULL       | NULL

As you can see, the LEFT JOIN returns all rows from the left table (Customers), including the unmatched row (David) with NULL values in the columns from the right table (Orders).

With dbForge Query Builder for SQL Server, you can easily select the desired join type (including LEFT JOIN or LEFT OUTER JOIN) and visually construct your queries without needing to write the SQL code manually.

  • 1
    Welcome to Stack Overflow! Both of your answers appear likely to have been written (entirely or partially) by AI (e.g., ChatGPT). As a heads-up, [posting of AI-generated content is not permitted on Stack Overflow](//meta.stackoverflow.com/q/421831). If you used an AI tool for assistance on this answer, could I ask you to (1) Reply in a comment here confirming that it was AI-generated, along with what tool (e.g., ChatGPT, Bing Chat, Copilot, etc.) -- We're compiling data on AI-assisted answers and could use your help. (2) After commenting, I recommend deleting your answer. Thanks! – NotTheDr01ds Jun 24 '23 at 11:38
  • 1
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 24 '23 at 11:38