I stumbled upon this T-SQL query created on a SQL Server 2005 database:
Select s_bs.ScheduleHeaderId,
s_bs.ScheduleBatchScheduleId,
FlowRateOperational.FlowRate,
BatchScheduleFlowRateOperational.EffectiveStartTime,
BatchScheduleFlowRateOperational.EffectiveEndTime
From BatchSchedule as bs
Inner Join ConnectionPoint as cp on bs.ConnectionPointId = cp.ConnectionPointId
Inner Join ScheduleBatch as s_b
Inner Join ScheduleConnectionPoint as s_cp
Inner Join ScheduleBatchSchedule as s_bs
on s_cp.ScheduleConnectionPointId = s_bs.ScheduleConnectionPointId
on s_b.ScheduleBatchId = s_bs.ScheduleBatchId
on cp.ConnectionPointName = s_cp.ConnectionPointName and bs.BatchID = s_b.BatchID
Inner Join BatchScheduleFlowRateOperational on bs.BatchScheduleId = BatchScheduleFlowRateOperational.BatchScheduleId
Inner Join FlowRateOperational on BatchScheduleFlowRateOperational.FlowRateOperationalId = FlowRateOperational.FlowRateOperationalId
I am not a SQL expert by far but at least I think I know how to join tables and I had never seen this way of joining tables before.
Is having several ON clauses together after their JOINS producing different results or increasing performance?
Why couldn’t this person just move the joins around and keep the ON clauses beside their corresponding JOIN?
Thanks for any light you can shed on this “mystery” 🙂
The
ONclause can be placed right after eachINNER JOIN(which looks nicer to me). But sometimes it’s inevitable to put theONclauses for the first couple of joins after a later join. For example the table which provides the join condition is not yet available in theJOINchain, for example:Look that both
ONclauses are referring toCso the associatedONclause could not appear earlier. It can be prevented by puttingCfirst, but depending on the usage, one may find it less readable.The above is just from the syntax point of view. There should be some performance considerations as well, which I’m not aware of.