I am really confused on RIGHT OUTER JOINs
and LEFT OUTER JOINs
. I am really confused on how to use them properly. The only join I know properly in the INNER JOIN
. I am going to ask some silly questions, but I have ask them in order for me to understand them properly.
How do i know which table would go on the right and on the left. Is that determined by which join it is?
I am asking all this questions because I am using the AdventureWorks Databse and I was doing a LEFT JOIN on the query below
Select SalesLT.Customer.CompanyName, SalesLT.SalesOrderHeader.SubTotal, SalesLT.SalesOrderHeader.TaxAmt
FROM SalesLT.Customer
LEFT OUTER JOIN SalesLT.SalesOrderHeader
ON SalesLT.Customer.CustomerID = SalesLT.SalesOrderHeader.CustomerID
This is the result I get
A Bike Store NULL NULL
Progressive Sports NULL NULL
Advanced Bike Components NULL NULL
Modular Cycle Systems NULL NULL
Metropolitan Sports Supply NULL NULL
Aerobic Exercise Company NULL NULL
Associated Bikes NULL NULL
Rural Cycle Emporium NULL NULL
Sharp Bikes NULL NULL
Bikes and Motorbikes NULL NULL
In the same query, I replaced the Left Outer join Join with RIght Outer Join and I got the below result
Professional Sales and Service 39785.3304 3182.8264
Remarkable Bike Store 6634.2961 530.7437
Bulk Discount Store 88812.8625 7105.029
Coalition Bike Company 2415.6727 193.2538
Futuristic Bikes 246.7392 19.7391
Channel Outlet 550.386 44.0309
Aerobic Exercise Company 2137.231 170.9785
Vigorous Sports Store 1059.31 84.7448
I am really confused. Please explain to me what's happening hereee. It could be because I have not done the join properly. If I am wrong, correct me.
THANK YOU
If a
JOIN
find matches for all rows then yes, it will look like anINNER JOIN
. TheOUTER
part of joins is about what happens when a match cannot be found.And the
LEFT
orRIGHT
is saying which table's rows we always want to retain. So in aLEFT
join, you'll always get all rows from the table to the left of the join, but for rows with no match on the right, we getNULL
s. And for aRIGHT
join, we always get all rows from the table to the right.And as I say, if you're doing a
LEFT
join and every row in the left table has at least one matching row in the right table, the result will look the same as anINNER JOIN
.