247

I have a very basic LEFT OUTER JOIN to return all results from the left table and some additional information from a much bigger table. The left table contains 4935 records yet when I LEFT OUTER JOIN it to an additional table the record count is significantly larger.

As far as I'm aware it is absolute gospel that a LEFT OUTER JOIN will return all records from the left table with matched records from the right table and null values for any rows which cannot be matched, as such it's my understanding that it should be impossible to return more rows than exist in the left table, but it's happening all the same!

SQL Query follows:

SELECT     SUSP.Susp_Visits.SuspReason, SUSP.Susp_Visits.SiteID
FROM         SUSP.Susp_Visits LEFT OUTER JOIN
                      DATA.Dim_Member ON SUSP.Susp_Visits.MemID = DATA.Dim_Member.MembershipNum

Perhaps I have made a mistake in the syntax or my understanding of LEFT OUTER JOIN is incomplete, hopefully someone can explain how this could be occurring?

6
  • 7
    To "get as many records returned as exist in the left table", you need to specify which row from the right side to choose if there are multiple matches.
    – A-K
    May 27, 2009 at 15:54
  • 1
    how do you specify this? I'd like the first match to be returned. Feb 4, 2013 at 19:03
  • 1
    you have to define what is meant by the first match. Do you want the earliest record , the one with the highest id or what?
    – HLGEM
    Oct 24, 2013 at 21:39
  • 3
    If you match with primary key in additional table your statement is correct. Oct 1, 2015 at 7:44
  • I often use a resource like this as a cheat sheet when building queries. If the link ever dies, just google sql join; they are Venn diagrams of the different types of join.
    – Zimano
    Jan 23, 2020 at 15:34

14 Answers 14

251

The LEFT OUTER JOIN will return all records from the LEFT table joined with the RIGHT table where possible.

If there are matches though, it will still return all rows that match, therefore, one row in LEFT that matches two rows in RIGHT will return as two ROWS, just like an INNER JOIN.

EDIT: In response to your edit, I've just had a further look at your query and it looks like you are only returning data from the LEFT table. Therefore, if you only want data from the LEFT table, and you only want one row returned for each row in the LEFT table, then you have no need to perform a JOIN at all and can just do a SELECT directly from the LEFT table.

2
  • 5
    The reason for joining to the right table was so I only got records from the left where there was at least one record in the right table but thank you so much for the explanation.
    – Jay Wilde
    Sep 6, 2018 at 17:43
  • @JayWilde in that case you should rewrite your query to a WHERE EXISTS instead of a join. This is much, much faster, as the exists only needs to perform a single key lookup for each record in the left table and not return any data.
    – Tom Lint
    Aug 3, 2022 at 9:58
187
Table1                Table2
_______               _________
1                      2
2                      2
3                      5
4                      6

SELECT Table1.Id, 
       Table2.Id 
FROM Table1 
LEFT OUTER JOIN Table2 ON Table1.Id=Table2.Id

Results:

1,null
2,2
2,2
3,null
4,null
0
62

It isn't impossible. The number of records in the left table is the minimum number of records it will return. If the right table has two records that match to one record in the left table, it will return two records.

2
  • The alternate case will be happened for RIGHT JOIN or FULL JOIN? Oct 20, 2020 at 19:37
  • so it is possible...
    – Jwan622
    Feb 3 at 20:30
22

In response to your postscript, that depends on what you would like.

You are getting (possible) multiple rows for each row in your left table because there are multiple matches for the join condition. If you want your total results to have the same number of rows as there is in the left part of the query you need to make sure your join conditions cause a 1-to-1 match.

Alternatively, depending on what you actually want you can use aggregate functions (if for example you just want a string from the right part you could generate a column that is a comma delimited string of the right side results for that left row.

If you are only looking at 1 or 2 columns from the outer join you might consider using a scalar subquery since you will be guaranteed 1 result.

2
  • 7
    This is a good answer since it offered suggestions on how to return just rows from left table.
    – karns
    Mar 3, 2015 at 20:01
  • Alternatively, depending on what you actually want you can use aggregate functions (if for example you just want a string from the right part you could generate a column that is a comma delimited string of the right side results for that left row. This is exactly what I am trying to achieve, but have not figured out how to.
    – Bruni
    Dec 2, 2021 at 13:52
16

Each record from the left table will be returned as many times as there are matching records on the right table -- at least 1, but could easily be more than 1.

9

Could it be a one to many relationship between the left and right tables?

0
8

LEFT OUTER JOIN just like INNER JOIN (normal join) will return as many results for each row in left table as many matches it finds in the right table. Hence you can have a lot of results - up to N x M, where N is number of rows in left table and M is number of rows in right table.

It's the minimum number of results is always guaranteed in LEFT OUTER JOIN to be at least N.

3
  • 1
    I started to think when the number of rows is equal N x M and the only real situation which comes to my mind is when N or M equals 1. Do you agree? Sep 26, 2013 at 13:15
  • 2
    No, I don't. You shouldn't think of join condition as key equality join only. It can be arbitrary condition, e.g. date ranges, inequalities, etc. Two extreme cases: (a) N rows have not a single match among M rows, then left outer join results in N rows matched up with NULLs. (b) every of N rows matches all of M rows, then result is N x M rows set.
    – topchef
    Sep 26, 2013 at 16:12
  • 1
    You're right, I was thinking about joins only in terms of key equality. I like your example from "case b". I believe that "every of N rows matches all of M rows" is a general recipe for when N x M rows are returned, which is rather impossible to visualize when thinking about key equality only. Sep 27, 2013 at 11:24
8

If you need just any one row from the right side

SELECT SuspReason, SiteID FROM(
    SELECT SUSP.Susp_Visits.SuspReason, SUSP.Susp_Visits.SiteID, ROW_NUMBER()
    OVER(PARTITION BY SUSP.Susp_Visits.SiteID) AS rn
    FROM SUSP.Susp_Visits
    LEFT OUTER JOIN DATA.Dim_Member ON SUSP.Susp_Visits.MemID = DATA.Dim_Member.MembershipNum
) AS t
WHERE rn=1

or just

SELECT SUSP.Susp_Visits.SuspReason, SUSP.Susp_Visits.SiteID
FROM SUSP.Susp_Visits WHERE EXISTS(
    SELECT DATA.Dim_Member WHERE SUSP.Susp_Visits.MemID = DATA.Dim_Member.MembershipNum
)
1
  • 1
    Because you did not provide DDL and DML, I did not test. Anyway I think that EXISTS is what you want. Try this: SELECT SuspReason, SiteID FROM( SELECT SUSP.Susp_Visits.SuspReason, SUSP.Susp_Visits.SiteID, ROW_NUMBER() OVER(PARTITION BY SUSP.Susp_Visits.SiteID ORDER BY SUSP.Susp_Visits.SiteID) AS rn FROM SUSP.Susp_Visits LEFT OUTER JOIN DATA.Dim_Member ON SUSP.Susp_Visits.MemID = DATA.Dim_Member.MembershipNum ) AS t WHERE rn=1
    – A-K
    May 27, 2009 at 16:22
5

Pay attention if you have a where clause on the "right side' table of a query containing a left outer join... In case you have no record on the right side satisfying the where clause, then the corresponding record of the 'left side' table will not appear in the result of your query....

2
  • 1
    You should then add the condition to the ON clause of the corresponding LEFT OUTER JOIN.
    – Mik
    Mar 21, 2017 at 10:30
  • The WHERE clause is not part of the 'right side' or 'left side' of the SELECT statement, it belongs to the set of all the joins and therefore applies to the result of all the joins (i.e., after joining, not during joining). Just like @Mik says, such conditions that only apply to the 'left' or 'right' side should be part of the ON clause of a JOIN statement.
    – Abel
    Apr 19, 2021 at 22:08
2

It seems as though there are multiple rows in the DATA.Dim_Member table per SUSP.Susp_Visits row.

2

if multiple (x) rows in Dim_Member are associated with a single row in Susp_Visits, there will be x rows in the resul set.

1

The only way your query would return more number of rows than the left table ( which is SUSP.Susp_Visits in your case), is that the condition (SUSP.Susp_Visits.MemID = DATA.Dim_Member.MembershipNum) is matching multiple rows in the right table, which is DATA.Dim_Member. So, there are multiple rows in the DATA.Dim_Member where identical values are present for DATA.Dim_Member.MembershipNum. You can verify this by executing the below query:

select DATA.Dim_Member.MembershipNum, count(DATA.Dim_Member.MembershipNum) from DATA.Dim_Member group by DATA.Dim_Member.MembershipNum

1
  • 1
    Thanks! Never though of that Mar 15 at 20:13
0

Since the left table contains 4935 records, I suspect you want your results to return 4935 records. Try this:

create table table1
(siteID int, 
SuspReason int)

create table table2
(siteID int, 
SuspReason int)

insert into table1(siteID, SuspReason) values 
(1, 678), 
(1, 186), 
(1, 723)
    
insert into table2(siteID, SuspReason) values 
(1, 678),
(1, 965)
   
select distinct t1.siteID, t1.SuspReason
from table1 t1 left join table2 t2 on t1.siteID = t2.siteID and t1.SuspReason = t2.SuspReason

union 

select distinct t2.siteID, t2.SuspReason 
from table1 t1 right join table2 t2 on t1.siteID = t2.siteID and t1.SuspReason = t2.SuspReason
1
  • 2
    The question was about two tables that have distinct columns, where one adds auxiliary data to the other. What you are doing is applying a union to two tables that have the same structure, which is not what happens in OP's question.
    – Abel
    Apr 19, 2021 at 22:13
0

Simply, LEFT OUTER JOIN is the Cartesian product within each join key, along with the unmatched rows of the left table (i.e. for each key_x that has N records in table_L and M records in table_R the result will have N*M records if M>0, or N records if M=0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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