Reputation:
When you do natural join on the two tables above using this query:
Select name, course_id
from instructor natural join teaches;
In the above example, is instructor the left table and teaches the right table? Why is it that the first row in the result relation it has Srinivasan as the name. Shouldn't it have Einstein since that is the first row in the left table (instructor) and you are joining that with the right table (teaches)? So it will look at 222222 ID from instructor and see if that matches an ID from teaches, then it will look if 12121 (Wu) matches an ID from teaches, and so on. I am confused on how this output table is formed?
Upvotes: 4
Views: 2822
Reputation: 114
A natural join automatically selects columns from each table to use in determining matching rows. The SQL identifies columns in each table that have the same name and type; After that, rows in which the values of these columns are equal are returned as matching rows.
So, only after the common column is selected and sorted, in this case - id, the matching happens.
Hence the first name is Srinivasan, not Einstein.
Upvotes: 3