Hi to all,
Beginner question here...
Does the order of precedence of the tables matter if you are joining a number of tables together?
Lets take for example:
I have 3 tables to join:
Table 1 is "members" table
Table 2 is "companies" table
Table 3 is "units" table
"members" table contains the foreign key field of "companies" table which is named "company_id", it also contains the foreign key field of the "units" table and is named "unit_id".
I would join "companies" table and "units" table in order to retrieve their "company name" field and "unit name" field respectively.
This is my query:
Code:
SELECT
member_id,
surname,
first_name,
middle_name,
company_name,
unit_name,
year_hired
FROM members
INNER JOIN companies ON members.company_id = companies.company_id
INNER JOIN units ON members.unit_id = units.unit_id;
What if I rewrite it to this query?
Code:
SELECT
member_id,
surname,
first_name,
middle_name,
company_name,
unit_name,
year_hired
FROM units
INNER JOIN members ON units.unit_id = members.unit_id
INNER JOIN companies ON companies.company_id = members.company_id;
1.) Is this query also the same as the first query if I have change the order of the tables, I mean, will that produce the same result as my very first query?
2.) Is there a speed difference if I change the order of the tables?
3.) How about the order of the fields? Example if this is the join clause:
Code:
INNER JOIN members ON units.unit_id = members.unit_id
Is this also the same if you would rewrite it as:
Code:
INNER JOIN members ON members.unit_id = units.unit_id
I just switch the position of the "members.unit_id" field and "units.unit_id" field that are used for equality checking
Btw, I have also read some articles in the net that if you are using OUTER JOINS, then the precedence of the tables in your query does matter.
many thanks!