I'm working with some views in an old HR system that frequently puts a number of joins together with the 'ON' part of the joins not coming until the end. Why would someone do it in this way and is there some advantage to it? I find it quite confusing when there is a large number of joins in it. I couldn't quite describe the situation in a web search well enough to help me.
SELECT
ExportTypeIdNo = et.ExportTypeIdNo,
ExportDataStoredProcedureIdNo = et.ExportDataStoredProcedureIdNo,
ExportDataStoredProcedure = sp1.Name,
ExportFileStoredProcedureIdNo = et.ExportFileStoredProcedureIdNo,
ExportFileStoredProcedure = sp2.Name
FROM tSTORED_PROCEDURES sp2 INNER JOIN
(tSTORED_PROCEDURES sp1 INNER JOIN
(tEXPORT_TYPES et INNER JOIN tTYPE_CODES tc
ON et.ReportingTreeIdNo = tc.TypeCodeIdNo)
ON sp1.StoredProcedureIdNo = et.ExportDataStoredProcedureIdNo)
ON sp2.StoredProcedureIdNo = et.ExportFileStoredProcedureIdNo
This code is just an example of what this type of join looks like.
There is no good reason to do this. With inner join
, any ordering of the join
s is equivalent. Almost everyone would agree that the following is simpler to follow and maintain:
FROM tEXPORT_TYPES et INNER JOIN
tTYPE_CODES tc
ON et.ReportingTreeIdNo = tc.TypeCodeIdNo INNER JOIN
tSTORED_PROCEDURES sp1
ON sp1.StoredProcedureIdNo = et.ExportDataStoredProcedureIdNo INNER JOIN
tSTORED_PROCEDURES sp2
ON sp2.StoredProcedureIdNo = et.ExportFileStoredProcedureIdNo
There are some arcane situations where rearranging outer joins is not exactly equivalent. In general, though, inner join
s followed by left join
s is sufficient for almost all the queries that I write.
As to why someone would write the join
s this way? I can only speculate. My most likely reason is that they were using ,
s in the from
clause and simply grew to prefer having all the tables referenced before the conditions connecting them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With