Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this type of join called where the joins are qualified at the end?

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.

like image 977
Randall W Thomson Avatar asked Jan 22 '19 00:01

Randall W Thomson


1 Answers

There is no good reason to do this. With inner join, any ordering of the joins 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 joins followed by left joins is sufficient for almost all the queries that I write.

As to why someone would write the joins 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.

like image 184
Gordon Linoff Avatar answered Nov 15 '22 05:11

Gordon Linoff