There\'s a big discussion going on at my office on the order of the joined columns in a sql join. I\'m finding it hard explaining it so I\'ll just present the two sql statem
I have always used option 1 and that is also the way that orcale teach it on their SQL and PL/SQL course.
On the subject of best practice though,
One thing you have used that oracle also teach is the 1 or 2 letter table aliases.. Although this can seem like a great idea at the time and it makes writing the statement / procedure / function allot easier (at the time) it can be difficult to then come back and look at the same piece of code at a later date.
e.g. We have many PL/SQL functions that are looking at / pulling data from tens of tables and while the aliases are fresh in your mind , great, but come back to the same code in a few years and it can get sticky.
I'm not saying don't use table aliases but i always try and avoid the 1 or 2 letter ones that allot of places / people teach.
(Not quite on the topic of joins only but on the topic of best practice)
SQL was designed to be readable as usual English text. In a usual text, when we want to specify any mentioned object, we say something like "An object. This object is like that object, this object is related to that object, and this object has those properties." We do not say "That object is like this object, to that object related this object and those properties have this object".
So, when we adding records of TableB to query from TableA, we need to specify what must have a record from TableB to satisfy current record from TableA. So we must say, wee need that records from TableB, which have an id, equal to TableA.ID. FROM TableA a JOIN TableB b ON b.col = a.col
. We must write exactly in this order, to provide easy reading and understanding of this relation.
And I'm very very sad, that almost all SQL documentation does not provide any kind of reasoning to go one or another direction in this question, but have a lot of examples of wrong writing equation arguments in order of presenting of tables in a query.
Refer to the below link, where it concludes that the order in ON clause doesn't affect the result. The database optimizer will decide the best way and plan to execute the query.
http://weblogs.sqlteam.com/joew/archive/2008/02/29/60542.aspx
The best practice here is to choose one and stick with it within the team. Personally, I prefer the FROM a JOIN b ON b.col = a.col
because it seems cleaner to me.
Doesn't make a difference, the database query optimiser will decide on the best way to execute the query regardless of how you order your join conditions, or even your where clauses.
The way the oracle optimiser handles joins is explained in quite a bit of detail at http://docs.oracle.com/cd/B10501_01/server.920/a96533/optimops.htm#39473.