Overview: In this tutorial you will learn and understand what is LEFT join in SQL with examples.
Introduction
LEFT join in SQL returns all row records from the left side of table and only those rows from the other table where the joined fields are equal. Table name mentioned in SELECT statement after FROM keyword is always consider as Left table in LEFT join.
Syntax:
SELECT
column1, Column2, ....
FROM
left_table
LEFT [OUTER] JOIN Right_table
ON left_table.columnName = Right_table.columnName;
Example #1: SELECT command for LEFT join
So lets understand this with example of two table as shown below tables
left table is Customers_Tbl and right table is Orders_Tbl
Customer_ID | Customer_Name | |
---|---|---|
121 | Deep | Deep@a.cm |
123 | Robert | Robert@a.cm |
521 | Markus | Markus@a.cm |
500 | Kumar | Kumar@a.cm |
200 | Ravi | Ravi@a.cm |
300 | Daniel | Daniel@a.cm |
Customer_ID | Order_Date | Order_Quantity |
---|---|---|
500 | 4/15/2020 | 3456 |
521 | 6/19/2021 | 3000 |
600 | 8/15/2019 | STV |
200 | 9/25/2018 | 2300 |
We will apply the LEFT join on Customer table and below syntax will bring all common Customer ID from right table into the Left table as shown below
So you can see yellow marked rows are common in both table so only yellow marked rows will start appearing in front of Customer ID which is matched.
SELECT
c.Customer_ID, c.Customer_Name,
o.Customer_ID, o.Order_Quantity
FROM
Customers_Tbl c
LEFT JOIN Orders_Tbl o
ON c.Customer_ID = o.Customer_ID;
The new output table will look like as shown below table
Customer_ID | Customer_Name | Customer_ID | Order_Quantity | |
---|---|---|---|---|
121 | Deep | Deep@a.cm | NULL | NULL |
123 | Robert | Robert@a.cm | NULL | NULL |
521 | Markus | Markus@a.cm | 521 | 3456 |
500 | Kumar | Kumar@a.cm | 500 | 3000 |
200 | Ravi | Ravi@a.cm | 200 | 2300 |
300 | Daniel | Daniel@a.cm | NULL | NULL |
So Green column is from right table that is Order_Tbl and bring all matched rows based on Customer_ID condition.
Example #2: Update Using LEFT join in SQL
How you can update your table using join in SQL. Sometime you need to bring few records from another table to merge data in one table you can say few mapping you need to do in main table using Mapping table.
So let see with example how you can update SQL table using UPDATE Command in Left Join.
Let have two table Employee and Country, you want to bring employee’s country name from another table to main employee table. So that the blank Country_name column will reflects the country name.
Employee_ID | Employee_name | Country_name |
---|---|---|
2001 | Deep | |
2002 | Markus | |
2005 | Robert | |
2007 | Kumar | |
2010 | Mathew |
Employee_ID | Country_name |
---|---|
2001 | USA |
2005 | Germany |
2007 | India |
2009 | UK |
Below Query you can use to update Employee table
Update
Employee
SET Employee.Country = Country.Country_name
FROM
Employee
LEFT JOIN Country
ON Employee.Customer_ID = Country.Customer_ID
Output of above query will be
Employee_ID | Employee_name | Country_name |
---|---|---|
2001 | Deep | USA |
2002 | Markus | NULL |
2005 | Robert | Germany |
2007 | Kumar | India |
2010 | Mathew | NULL |
Example #3: LEFT join SQL WHERE clause
you can also use condition to put on JOIN in SQL to bring few columns based on condition and you can use WHERE clause to make this happen,
lets take example of below tables, and we would like to bring those common Customer_ID which have more then 2000 Order_Quantity
Customer_ID | Customer_Name | |
---|---|---|
121 | Deep | Deep@a.cm |
123 | Robert | Robert@a.cm |
521 | Markus | Markus@a.cm |
500 | Kumar | Kumar@a.cm |
200 | Ravi | Ravi@a.cm |
300 | Daniel | Daniel@a.cm |
Customer_ID | Order_Date | Order_Quantity |
---|---|---|
500 | 4/15/2020 | 3456 |
521 | 6/19/2021 | 3000 |
600 | 8/15/2019 | STV |
200 | 9/25/2018 | 2300 |
SELECT
c.Customer_ID, c.Customer_Name,
o.Customer_ID, o.Order_Quantity
FROM
Customers_Tbl c
LEFT JOIN Orders_Tbl o
ON c.Customer_ID = o.Customer_ID;
WHERE o.Order_Quantity > '2000'
Then the Output will be look like below table
Customer_ID | Customer_Name | Customer_ID | Order_Quantity | |
---|---|---|---|---|
121 | Deep | Deep@a.cm | NULL | NULL |
123 | Robert | Robert@a.cm | NULL | NULL |
521 | Markus | Markus@a.cm | 521 | 3456 |
500 | Kumar | Kumar@a.cm | 500 | 3000 |
200 | Ravi | Ravi@a.cm | NULL | NULL |
300 | Daniel | Daniel@a.cm | NULL | NULL |
So with this condition it will bring only 2 rows which have more then 2000 order quantity.