July 24, 2023 at 12:00 am
Comments posted to this topic are about the item The Left Joins
July 31, 2023 at 10:34 am
To select all rows from CustomerLeft change join precedence by parentheses:
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN (dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed ON ed.CustomerEmail = cc.CustomerEmail)
ON (cc.CustomerID = cl.CustomerID)
July 31, 2023 at 1:31 pm
To select all rows from CustomerLeft change join precedence by parentheses:
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN (dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed ON ed.CustomerEmail = cc.CustomerEmail)
ON (cc.CustomerID = cl.CustomerID)
It's actually the order of the ON
clauses that determines the JOIN
order, so the parens are NOT required. They only help humans understand the order. The following will also show all records from CustomerLeft.
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed
ON ed.CustomerEmail = cc.CustomerEmail
ON cc.CustomerID = cl.CustomerID
Here is an alternate format that might be easier for humans to parse.
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed
ON ed.CustomerEmail = cc.CustomerEmail
ON cc.CustomerID = cl.CustomerID
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 31, 2023 at 1:39 pm
Carlo Romagnano wrote:To select all rows from CustomerLeft change join precedence by parentheses:
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN (dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed ON ed.CustomerEmail = cc.CustomerEmail)
ON (cc.CustomerID = cl.CustomerID)It's actually the order of the
ON
clauses that determines theJOIN
order, so the parens are NOT required. They only help humans understand the order. The following will also show all records from CustomerLeft.SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed
ON ed.CustomerEmail = cc.CustomerEmail
ON cc.CustomerID = cl.CustomerIDHere is an alternate format that might be easier for humans to parse.
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN dbo.CustomerContact AS cc
INNER JOIN dbo.EmailDomain AS ed
ON ed.CustomerEmail = cc.CustomerEmail
ON cc.CustomerID = cl.CustomerIDDrew
I have never found the need to nest joins like that. I think this might be a more appropriate join:
SELECT *
FROM dbo.CustomerLeft AS cl
LEFT JOIN dbo.CustomerContact AS cc ON cc.CustomerID = cl.CustomerID
LEFT JOIN dbo.EmailDomain AS ed ON ed.CustomerEmail = cc.CustomerEmail
;
The results are slightly different and contain more information
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply