mysqljoin

SQL Inner Join and Natural Join


I'm working on an old exam paper in preparation for an exam.

Inner Join and Natural Join:

image

Select * From r INNER JOIN s ON r.F=s.F

Why when doing an Inner Join does 12 get removed in row E and not 20?

Select E, G From r NATURAL JOIN s;

Is 12 removed from row E and not 20?


Solution

  • Select * From r INNER JOIN s ON r.F=s.F
    

    result will be

    E  F  F  G
    10 5  5 30
    20 15 15 20
    

    The INNER JOIN keyword selects records that have matching values in both tables. This is why 12 is removed ie, no common column. Now the result for

    Select E, G From r NATURAL JOIN s
    

    is

    E  G
    10 30
    20 20
    

    when you use * on select you get

    F  E   G
    5  10 30
    15 20 20
    

    while using * on natural join it will be expanded to the following list of columns