I run SQL Server 2008 and I'm trying to execute following SQL query:
Query 1:
SELECT *
FROM tableA
LEFT OUTER JOIN tableB AS tabX ON tabX.some_id = tableA.some_id
LEFT OUTER JOIN tableB AS tabY ON tabY.some_id = tableA.some_id
WHERE tabX.some_attribute = 'X'
AND tabY.some_attribute = 'Y'
I already know that conditions after WHERE
statement do mess up LEFT OUTER JOIN
and generally make it behave like a classic INNER JOIN
. What's interesting, it's not the case on SQL Server 2005.
To fix it I can do this:
Query 2:
SELECT *
FROM tableA
LEFT OUTER JOIN tableB AS tabX ON tabX.some_id = tableA.some_id AND tabX.some_attribute = 'X'
LEFT OUTER JOIN tableB AS tabY ON tabY.some_id = tableA.some_id AND tabY.some_attribute = 'Y'
Basically I have to include WHERE
conditions within ON
statements and the query will execute the way it was intended.
My first question is: Why doesn't SQL Server interpret both queries same way (as older SQL Server versions or Oracle DBServer do)?
I ask because I'm confused about how and why conditions (after WHERE
statement) from first query affect the main logical repertory (by that I mean "main results"). Especially as both conditions specifically refer to aliases tabX and tabY
My second question is: Can I somehow change this behaviour? (e.g. in Server Configuration ?)
Best regards, Piotr