T-SQL Language – RIGHT or LEFT JOIN Differences

image I was asked if LEFT JOIN can do the same task as RIGHT JOIN by reserving the order of the tables in join, why does RIGHT JOIN exists?

The definitions are as following:

LEFT JOIN – select all the records from the LEFT table and then pick up any matching records from the RIGHT table

RIGHT JOIN – select all the records from the RIGHT table and then pick up any matching records from the LEFT table

Most of us read from LEFT to RIGHT so we are using LEFT join. Do you have any explanation why RIGHT JOIN exists or can you come up with example, where RIGHT JOIN is absolutely required and the task can not be achieved with LEFT JOIN.

Sample:

USE AdventureWorks
SELECT
PD.ProductID,
PD.Name,
PD.ProductNumber,
PM.Name AS Product_Model,
PCH.StandardCost
FROM Production.Product PD
LEFT OUTER JOIN Production.ProductModel PM
ON PD.ProductModelID = PM.ProductModelID
LEFT OUTER JOIN Production.ProductCostHistory PCH
ON PD.ProductID = PCH.ProductID

About onlinejt

Blogger

No comments yet.

Leave a Reply

*