I’d like to know if having to conditionals when using a JOIN keyword is a good practice.
I’m trying to filter this resultset by date but I’m unable to get all the branches listed even if there’s no expense or income for a date using a WHERE clause. Is there a better way of doing this, if so how?
SELECT
Branches.Name
,SUM(Expenses.Amount) AS Expenses
,SUM(Incomes.Amount) AS Incomes
FROM
Branches
LEFT JOIN Expenses
ON Branches.Id = Expenses.BranchId AND Expenses.Date = '3/11/2010'
LEFT JOIN Incomes
ON Branches.Id = Incomes.BranchId AND Incomes.Date = '3/11/2010'
GROUP BY Branches.Name
Why not? Even more!
OUTER JOIN has very specific trick about these two conditions!
INNER JOIN is tolerant to recombination, for example following are equivalent:
via:
BUT!!! For OUTER JOIN you MUST specify exactly two conditions inside ON, since WHERE would treat result as INNER JOIN