2

I'm working through the JOIN tutorial on SQL zoo.

Let's say I'm about to execute the code below:

SELECT a.stadium, COUNT(g.matchid)
FROM game a 
JOIN goal g 
ON g.matchid = a.id
GROUP BY a.stadium 

As it happens, it produces the same output as the code below:

SELECT a.stadium, COUNT(g.matchid)
FROM goal g  
JOIN game a
ON g.matchid = a.id
GROUP BY a.stadium 

So then, when does it matter which table you assign at FROM and which one you assign at JOIN?

AdamMc331
  • 15,982
  • 9
  • 67
  • 128
zthomas.nc
  • 3,245
  • 6
  • 33
  • 46

5 Answers5

1

When you only JOIN 2 tables, usually the order does not matter: MySQL scans the tables in the optimal order.

When you scan more than 2 tables, the order could matter:

SELECT ...
FROM a
JOIN b ON ...
JOIN c ON ...

Also, MySQL tries to scan the tables in the fastest way (large tables first). But if a join is slow, it is possible that MySQL is scanning them in a non-optimal order. You can verify this with EXPLAIN. In this case, you can force the join order by adding the STRAIGHT_JOIN keyword.

Federico Razzoli
  • 4,500
  • 1
  • 18
  • 20
1

When you are using an INNER JOIN like you are here, the order doesn't matter. That is because you are connecting two tables on a common index, so the order in which you use them is up to you. You should pick an order that is most logical to you, and easiest to read. A habit of mine is to put the table I'm selecting from first. In your case, you're selecting information about a stadium, which comes from the game table, so my preference would be to put that first.

In other joins, however, such as LEFT OUTER JOIN and RIGHT OUTER JOIN the order will matter. That is because these joins will select all rows from one table. Consider for example I have a table for Students and a table for Projects. They can exist independently, some students may have an associated project, but not all will.

If I want to get all students and project information while still seeing students without projects, I need a LEFT JOIN:

SELECT s.name, p.project
FROM student s
LEFT JOIN project p ON p.student_id = s.id;

Note here, that the LEFT JOIN refers to the table in the FROM clause, so that means ALL of students were being selected. This also means that p.project will be null for some rows. Order matters here.

If I took the same concept with a RIGHT JOIN, it will select all rows from the table in the join clause. So if I changed the query to this:

SELECT s.name, p.project
FROM student s
RIGHT JOIN project p ON p.student_id = s.id;

This will return all rows from the project table, regardless of whether or not it has a match for students. This means that in some rows, s.name will be null. Similar to the first example, because I've made project the outer joined table, p.project will never be null (assuming it isn't in the original table). In the first example, s.name should never be null.

In the case of outer joins, order will matter. Thankfully, you can think intuitively with LEFT and RIGHT joins. A left join will return all rows in the table to the left of that statement, while a right join returns all rows from the right of that statement. Take this as a rule of thumb, but be careful. You might want to develop a pattern to be consistent with yourself, as I mentioned earlier, so these queries are easier for you to understand later on.

AdamMc331
  • 15,982
  • 9
  • 67
  • 128
0

The order doesn't always matter, I usually just order it in a way that makes sense to someone reading your query.

Sometime order does matter. Try it with LEFT JOIN and RIGHT JOIN.

Dan
  • 9,804
  • 4
  • 21
  • 35
0

In this instance you are using an INNER JOIN, if you're expecting a match on a common ID or foreign key, it probably doesn't matter too much.

You would however need to specify the tables the correct way round if you were performing an OUTER JOIN, as not all records in this type of join are guaranteed to match via the same field.

Hysteria86
  • 367
  • 1
  • 10
0

yes, it will matter when you will user another join LEFT JOIN, RIGHT JOIN currently You are using NATURAL JOIN that is return all tables related data, if JOIN table row not match then it will exclude row from result

If you use LEFT / RIGHT {OUTER} join then result will be different, follow this link for more detail

Community
  • 1
  • 1
Girish
  • 11,607
  • 3
  • 35
  • 49