In what order are MySQL JOINs evaluated?

  1. USING (fieldname) is a shorthand way of saying ON table1.fieldname = table2.fieldname.

  2. SQL doesn’t define the ‘order’ in which JOINS are done because it is not the nature of the language. Obviously an order has to be specified in the statement, but an INNER JOIN can be considered commutative: you can list them in any order and you will get the same results.

    That said, when constructing a SELECT … JOIN, particularly one that includes LEFT JOINs, I’ve found it makes sense to regard the third JOIN as joining the new table to the results of the first JOIN, the fourth JOIN as joining the results of the second JOIN, and so on.

    More rarely, the specified order can influence the behaviour of the query optimizer, due to the way it influences the heuristics.

  3. No. The way the query is assembled, it requires that companies and users both have a companyid, jobs has a userid and a jobid and useraccounts has a userid. However, only one of companies or user needs a userid for the JOIN to work.

  4. The WHERE clause is filtering the whole result — i.e. all JOINed columns — using a column provided by the jobs table.

Leave a Comment