What is the difference between Left, Right, Outer and Inner Joins?

后端 未结 9 1951
刺人心
刺人心 2020-11-22 07:10

I am wondering how to differentiate all these different joins ...

9条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 08:08

    Making it more visible might help. One example:

    Table 1:

    ID_STUDENT STUDENT_NAME

    1               Raony
    2               Diogo
    3               Eduardo
    4               Luiz
    

    Table 2:

    ID_STUDENT LOCKER

    3               l1
    4               l2
    5               l3
    

    What I get when I do:

    -Inner join of Table 1 and Table 2: 
    
        - Inner join returns both tables merged only when the key 
          (ID_STUDENT) exists in both tables
    
        ID_STUDENT       STUDENT_NAME      LOCKER   
    
            3               Eduardo          l1
            4               Luiz             l2
    
    -Left join of Table 1 and Table 2:
    
        - Left join merges both tables with all records form table 1, in 
          other words, there might be non-populated fields from table 2
    
        ID_ESTUDANTE    NOME_ESTUDANTE     LOCKER   
    
            1               Raony            -
            2               Diogo            -
            3               Eduardo          l1
            4               Luiz             l2
    
    -Right join of table 1 and table 2:
    
        - Right join merges both tables with all records from table 2, in 
          other words, there might be non-populated fields from table 1
    
        ID_STUDENT        STUDENT_NAME     LOCKER   
    
            3               Eduardo          l1
            4               Luiz             l2
            5               -                l3
    
    -Outter join of table 1 and table 2:
    
        - Returns all records from both tables, in other words, there
          might be non-populated fields either from table 1 or 2.
    
        ID_STUDENT        STUDENT_NAME     LOCKER   
            1               Raony            -
            2               Diogo            -
            3               Eduardo          l1
            4               Luiz             l2
            5               -                l3
    

提交回复
热议问题