In this scenario, all Contractors place an order for various Stock Items on a daily basis. This data is stored in the ContractorOrder table. When the Stock is received, the values are recorded and saved in the ContractorStock table. It is not possible for line items to appear in the ContractorStock table if they do not exist in the ContractorOrder table.
Sometimes however, some items are not delivered, or the incorrect quantities are delivered, or the Contractors forgot to place an order. To check this, I wrote these two queries (ignore the situation where the Sums are the same but individual line items differ - this is just a simplified example):
SELECT C.ContractorId
, C.ContractorName
, Sum(CO.Quantity) OrderQuantity
, Sum(CS.Quantity) ContractorQuantity
FROM [Contractor] C
LEFT JOIN ContractorOrder CO
ON C.ContractorId = CO.ContractorId
AND CO.Date = @date
LEFT JOIN
(
ContractorStock CS
JOIN StockCode SC
ON CS.StockCodeId = SC.StockCodeId
AND CS.Date = @date
)
ON CS.ContractorId = C.ContractorId
AND SC.StockCode = CO.StockCode
GROUP BY C.ContractorId
, C.ContractorName
http://sqlfiddle.com/#!6/55496/5
And:
SELECT C.ContractorId
, C.ContractorName
, Sum(CO.Quantity) OrderQuantity
, Sum(CS.Quantity) ContractorQuantity
FROM [Contractor] C
LEFT JOIN ContractorOrder CO
ON C.ContractorId = CO.ContractorId
AND CO.Date = @date
LEFT JOIN
(
SELECT CS.ContractorId
, SC.StockCode
, CS.Quantity
FROM ContractorStock CS
JOIN StockCode SC
ON CS.StockCodeId = SC.StockCodeId
AND CS.Date = @date
) CS
ON CS.ContractorId = C.ContractorId
AND CS.StockCode = CO.StockCode
GROUP BY C.ContractorId
, C.ContractorName
http://sqlfiddle.com/#!6/55496/2
Both of them return the same results, but there is a slight difference in the Execution Plans. Are these queries not logically equivalent in this situation? If they are, should I prefer the Nested Join?
Nested Join:
Sub Query: