ChatGPT解决这个技术问题 Extra ChatGPT

How can a LEFT OUTER JOIN return more records than exist in the left table?

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?

Postscript

Thanks for the great answers, my understanding of LEFT OUTER JOINS is now much better, could anyone however suggest a way this query could be modified so that I only get as many records returned as exist in the left table?

This query is purely to generate a report and the duplicate matches simply confuse matters.

/Postscript

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.
how do you specify this? I'd like the first match to be returned.
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?
If you match with primary key in additional table your statement is correct.
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.

R
Robin Day

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.


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.
E
Ergest Basha
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

H
HLGEM

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.


The alternate case will be happened for RIGHT JOIN or FULL JOIN?
C
Chris Cameron-Mills

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.


This is a good answer since it offered suggestions on how to return just rows from left table.
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.
A
Alex Martelli

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.


K
Ken Burkhardt

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


t
topchef

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.


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?
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.
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.
A
Anonymous

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
)

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
S
Serge

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....


You should then add the condition to the ON clause of the corresponding LEFT OUTER JOIN.
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.
b
bdukes

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


M
Manu

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.


u
user716255

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

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.
B
Binita Bharati

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