Reputation: 53
I have 2 tables:
Class (PK = Tutor)
Columns: ID / Tutor / Room
Teacher (PK FK = Tutor)
Columns: Tutor / Contact
I want to join both tables. Does it matter what order the tables are joined?
ie.
SELECT Class.ID, Class.Tutor, Teacher.Contact
FROM **Class INNER JOIN Teacher** ON Class.Tutor=Teacher.Tutor
or could I do it the other way round and Teacher INNER JOIN Class instead?
Both tables also share a common column name Tutor. Does it matter if I use Class.Tutor or Teacher.Tutor in the query?
Upvotes: 1
Views: 63
Reputation: 7401
For an INNER JOIN
, the order does not matter; the result set you will get will contain all the rows from both Class
and Teacher
where the common field Tutor
matches, whichever order you specify Teacher
and Class
.
When you use an OUTER JOIN
, the order does matter; I would recommend looking at the answers to What is the difference between Left, Right, Outer and Inner Joins?.
Upvotes: 3
Reputation: 377
Yes, the order matters. Selecting from class and joining Teacher will show only those classes that have a teacher Selecting from teacher and joining Class will show only those teachers that have a class
Depending on what you are trying to achieve you'll know which is most relevant.
I'd recommend using "left join" though, it will give you all the classes even if they don't have teacher assigned etc
FWIW "class" is likely to be a reserved word in your database, server side script and client side scripting languages. I'd find another word for that table name.
Upvotes: -2