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

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

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

相关标签:
9条回答
  • 2020-11-22 08:02

    SQL JOINS difference:

    Very simple to remember :

    INNER JOIN only show records common to both tables.

    OUTER JOIN all the content of the both tables are merged together either they are matched or not.

    LEFT JOIN is same as LEFT OUTER JOIN - (Select records from the first (left-most) table with matching right table records.)

    RIGHT JOIN is same as RIGHT OUTER JOIN - (Select records from the second (right-most) table with matching left table records.)

    0 讨论(0)
  • 2020-11-22 08:02

    Inner join: Only show rows, when has it data from both of the tables.

    Outer join: (left/right): Show the all result from the left / right table with the paired row(s), if it exists or not.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题