Left Outer Join Not Working?
You should move the constraints on prescriptions.filldate
into the ON
condition of the join, and remove it from the where
clause:
LEFT OUTER JOIN prescriptions ON prescriber.dea_no = prescriptions.dea_no
AND prescriptions.filldate >= '09-01-12'
AND prescriptions.filldate <= '09-17-12'
Otherwise, entries for which there are no prescriptions
end up with null
s in prescriptions.filldate
, and the WHERE
clause throws them away.
Here you can find a brief description about query processing phases (it's common for most DBMSes). You will find out there, that for OUTER JOIN:
- first CARTESIAN JOIN is produced,
- than the ON condition is performed on result set producing subset of rows,
- after than outer rows are appended with NULLs on inner table's joined columns,
- on that result the WHERE clause is applied performing filtering.
When you place the condition within WHERE clause which touches outer tables rows they're all discarded. You should simply place that condition within the ON clause, as that one is evaluated before outer rows are appended.
So, those conditions:
prescriptions.filldate >= '09-01-12' AND
prescriptions.filldate <= '09-17-12'
should be moved into ON clause.