How to Left Join Multiple Tables in SQL - GeeksforGeeks

06 Apr, 2021
Left Join is one of the Keywords used while writing queries in SQL. In SQL we normally use Join for the purpose of forming a new table by taking out common data like rows or records or tuples from both the tables which are having matching records in general. 
Here when it comes to Left Join in SQL it only returns all the records or tuples or rows from left table and only those records matching from the right table.
Syntax For Left Join:
SELECT column names
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
 Note: For example, if you have a left table with 10 rows, you are guaranteed to have at least 10 rows after applying join operation on two tables.
Consider two tables: 
1. Employee (Left Table) :
 2. Projects (Right Table) :
To Join these two tables and to obtain common information we need to use the following query
SELECT E.Emp_id, E.First_Name, E.Last_Name, E.Gender, E.age, E.Date_of_join,    
       P.date AS Project_Assigned_date, P.No_of_hours_worked AS hours_worked
FROM Employee E
LEFT JOIN Projects P
ON E.Emp_id = P.Emp_id
GROUP BY E.Emp_id;
After execution of query the obtained table will be like:
Once after obtaining the table as you can see that the Emp_id who is not assigned for a project who’s  Project_Assigned_date has became NULL and No_of_hours_worked also became NULL cause the Employee has not assigned anything to do.
Here Left Join mean in the sense based on above tables it took data from both the table rows which are matching and it also returned the values for the rows who’s data is not present in Table 2 as NULL cause we need to consider all the data of Left table.
Multiple LEFT JOIN’s in One Query:Sometimes you need to LEFT JOIN more than two tables to get the data required for specific analyses. Fortunately, the LEFT JOIN keyword can be used with MULTIPLE TABLES in SQL.
Consider a table called Salary:
Here we combine the data from these tables Employee, Projects and Salary.
To do this the query need to be written in the below format:
SELECT E.Emp_id, E.First_Name, E.Last_Name, E.Gender, E.age, E.Date_of_join,  
   P.No_of_hours_worked AS hours_worked, S.Salary_inc AS Salary_Increment
FROM Employee E
LEFT JOIN Projects P
ON E.Emp_id = P.Emp_id
LEFT JOIN Salary S
ON E.Emp_id = S.Emp_id;
And the resulting table looks like after multiple Left Join:
Hence you can see that we have combined the data from three tables into one single table using Left Join multiple times.
Consider one more Table called Experience:
Here we combine the data from these tables Employee, Projects and Experience.
To do this the query need to be written in the below format:
SELECT E.Emp_id, E.First_Name, E.Last_Name, P.date AS Project_Assigned_date, 
E1.Experience AS EXP
FROM Employee E
LEFT JOIN Projects
 P
ON E.Emp_id = P.Emp_id
LEFT JOIN Experience E1
ON E.Emp_id = E1.id;
And the resulting table looks like after multiple Left Join:
As you can see, the LEFT JOIN in SQL can be used with multiple tables. 
DBMS-SQL
Picked
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Update Multiple Columns in Single Update Statement in SQL?
How to Alter Multiple Columns at Once in SQL Server?
What is Temporary Table in SQL?
SQL using Python
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
SQL Query for Matching Multiple Values in the Same Column
SQL Query to Insert Multiple Rows
SQL Query to Convert VARCHAR to INT
SQL | Subquery
SQL - SELECT from Multiple Tables with MS SQL Server