RIGHT OUTER JOIN Example in SQL SERVER

This join returns all the rows from the right table with the matching rows from the left table. If there are no columns mathcing in the left table ,it returns NULL values.
    Use Right Outer Join when you only want to return rows that have matching data in the right table, even if there's no matching rows in the left table.

Ex:

     Left Table(emp Table)


    ------------------------------------------------------------------------
    empID    FirstName    LastName    UserName
    ------------------------------------------------------------------------
       1               John         Miller        John
       2               Steve         Jobs        Steve
       3          Billgates         john        Billgates
       4            Ozzy         Ozzbourne    Ozzy
       5            Homer         Gain             Homer
   --------------------------------------------------------------------------

     Right Table(dept Table)

  ------------------------------------------------------
    empID        DeptName
  ------------------------------------------------------
    1        IT
    2        Networking
    3        Networking
    4        Networking
    12        Admin
   ------------------------------------------------------
    
     Query :

     SELECT * FROM emp AS e
     RIGHT JOIN dept AS d
     ON e.empid = d.empid

    result:

    empID     FirstName    LastName        UserName    IndividualId    AccessLevel
        1            John             Miller                 John                1                      IT
        2            Steve             Jobs                  Steve               2        Networking
        3          Billgates         john                Billgates            3        Networking
        4           Ozzy             Ozzbourne           Ozzy             4        Networking
     NULL       NULL        NULL                    NULL         12        Admin

No comments:

Post a Comment