Skip to content
Advertisement

LEFT JOIN returning no results if a WHERE clause is added?

I’m trying to add the dbo.EmpHRSchedule.Type = ‘HOLIDAY’ entries to the Hours column only if they exist. To my understanding, a LEFT JOIN would only return entries if they match, and return NULL for non-matches. When I uncomment the AND ehrs.Type = 'HOLIDAY' line, no results are returned at all.

Why is this the case? Is there another way I should be going about this to sum the dbo.Timebill.Timebilled and dbo.EmpHRSchedule.AllocationDays (if they exist)?

Advertisement

Answer

When you include:

in the WHERE clause, you actually get an INNER join instead of a LEFT join because you get only the matching rows from the table ers and not any of the unmatched rows for which ehrs.Type would be NULL.
What you can do is move the condition to the ON clause of the LEFT join:

and you could also include the other conditions for the same table:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement