Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Left Join

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 679
    Comment on it

    Left Join:

    Left Join is used to join two tables and it return all rows from table 1 and matching rows from table 2. The result is NULL for unmatched rows for table 2. Left Join keyword is used to apply Left Join on two tables.

    Syntax for Left Join:

    SELECT columnname(s)
    FROM tablename1
    LEFT JOIN tablename2
    ON tablename1.columnname=tablename2.columnname;
    

    In the above syntax there is a column taken from both table on which left join is applied and this column must be same in both tables.

    Example of Left Join:

     EmpID          EmpName              City  
    -----------------------------------------
      1             Akhil            Haridwar 
      2             Shilpi           Dehradun   
      3             Neha             Selaqui      
      4             Rohit            Rajpur   
      5             Harshit          Rishikesh    
    
    
    
    
     EmpID              Department            Salary  
        -----------------------------------------
         1               PHP                 15000
         2               HTML                10000
         3               PHP                 15000
         4               ActionScript        18000    
    

    As we have two tables Employee table and department table and we are applying left join on it using EmpID column.

     SELECT  EmpID, EmpName, Department, Salary
     FROM EMPLOYEE
     LEFT JOIN Department
     ON EMPLOYEE.EmpID = Department.EmpID;
    

    Output for the above query will be like this:

     EmpID           EmpName            Department            Salary  
    ----------------------------------------------------------------------------
       1               Akhil               PHP                 15000
       2               Shilpi              HTML                10000
       3               Neha                PHP                 15000
       4               Rohit               ActionScript        18000 
       5               Harshit             NULL                NULL
    

    As we can see from the output for EmpID =5 there is no record in Department table so NULL values will be shown for the columns from Department table.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: