Does it matter what order we join the tables in a query?


#1

Question

Does it matter what order we join the tables in a query?

Answer

It depends.

When we join two tables, the rows of each table will be matched together based on their value in a common column specified by the ON clause.

For INNER JOINs, only rows that matched in both the left and right tables will be returned in the results - so it does not matter the order in which you join the tables.

However, for LEFT JOINs, all rows in the left (first) table will be returned, along with any matched rows from the right (second) table. In this case, it may matter which order you join the tables, as any unmatched rows from the right table are not returned in the results.

See here to help visualize this!