Difference between inner join and left join?


  • 0
    Y

    below is my code, if I change inner join instead of left join, it's output is null? why?

    select Person.Firstname, Person.lastname,Address.city, Address. state
    from Person
    left join Address
    on Person.PersonId = Address.PersonId


  • 0
    C

    probably 'person's don't have addresses in Address table. Left join joins the column ignoring if those data exist in another table, while inner join only joins the the same data.


  • 4
    S

    Example:

    """""""""""

    ID Name

    1 One

    2 Two

    """""""""""

    ID Address

    3 USA

    """""""""""

    With inner join, the join will only occur if the ID exists in both tables. Since no ID overlap in example, the result will be null.

    Result of inner join:

    ID, Name, Address

    null

    With left join, the information from first table (ie Person table in this problem) is always in the result. If the second table has matching IDs, then the info from the second table will be added after the information from the first table. If the second table does not have an ID that matches the ID from the first table, then null will be printed for where information from the second table was supposed to be added.

    Result of left join:

    ID, Name, Address

    1 One null

    2 Two null


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.