SQL Left Join

  • Join operation in SQL is used to combine multiple tables together into a single table.
  • If we use left join to combine two different tables, then we will get all the records from the left table. But we will get only those records from the right table, which have the corresponding key in the left table. Rest other records in the right table for which the common column value doesn’t match with the common column value of the left table; then, it is displayed as NULL.
  • Let us take a look at the syntax of writing a query to perform the left join operation in SQL.
SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1 LEFT JOIN TableName2 ON TableName1.ColumnName = TableName2.ColumnName;  

Now let us see take a deeper dive into the left join in SQL with the help of examples. All the queries in the examples will be written using the MySQL database.

Consider we have the following tables with the given data:

Table 1: employee

EmployeeIDEmployee_NameEmployee_Salary
1Arun Tiwari50000
2Sachin Rathi64000
3Harshal Pathak48000
4Arjun Kuwar46000
5Sarthak Gada62000
6Saurabh Sheik53000
7Shubham Singh29000
8Shivam Dixit54000
9Vicky Gujral39000
10Vijay Bose28000

Table 2: department

DepartmentIDDepartment_NameEmployee_ID
1Production1
2Sales3
3Marketing4
4Accounts5
5Development7
6HR9
7Sales10

Table 3: Loan

LoanIDBranchAmount
1B115000
2B210000
3B320000
4B4100000
5B5150000
6B650000
7B735000
8B885000

Table 4: Borrower

CustIDCustNameLoanID
1Sonakshi Dixit1
2Shital Garg4
3Swara Joshi5
4Isha Deshmukh2
5Swati Bose7
6Asha Kapoor10
7Nandini Shah9

Table 5: customer

Customer_IDNameAgeSalary
1Aryan Jain5156000
2Arohi Dixit2125000
3Vineet Garg2431000
4Ajeet Singh2332000
5Ravi Rathod2342000
6Paras Aggrawal2250000
7Sonakshi Kapadiya2428000
8Sonali Kapoor2882000

Table 6: orders

Order_IDOrder_DateCutomer_IDAmount
12012-01-2023000
22012-05-1822000
32012-06-2834000
42012-04-1145000
52012-05-0488000

Example 1:

Write a query to perform the left join operation considering the employee table as the left table and the department table as the right table.

Query:

mysql> SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name FROM employee e LEFT JOIN department d ON e.EmployeeID = d.Employee_ID;  

We have used the SELECT command to retrieve EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name present in the employee and department table. Then we have used the LEFT JOIN keyword to perform the left join operation on the employee and department table where ‘e’ and ‘d’ are aliases. These two tables are joined on the column EmployeeID which is present in both the tables.

You will get the following output:

EmployeeIDEmployee_NameEmployee_SalaryDepartmentIDDepartment_Name
1Arun Tiwari500001Production
2Sachin Rathi64000NULLNULL
3Harshal Pathak480002Sales
4Arjun Kuwar460003Marketing
5Sarthak Gada620004Accounts
6Saurabh Sheik53000NULLNULL
7Shubham Singh290005Development
8Shivam Dixit54000NULLNULL
9Vicky Gujral390006HR
10Vijay Bose280007Sales

EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name are retrieved from employee and department tables. All the records from the employee table are retrieved. Only those records that have a corresponding EmployeeID in the employee table are retrieved from the department table. Rest other records in the department table for which an EmployeeID doesn’t match with the EmployeeID of the employee table; then, it is displayed as NULL.

Example 2:

Write a query to perform the left join operation considering the loan table as the left table and the borrower table as the right table.

Query:

mysql> SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l LEFT JOIN Borrower b ON l.LoanID = b.LoanID;  

We have used the SELECT command to retrieve LoanID, Branch, Amount, CustID, CustName present in the loan and borrower table. Then we have used the LEFT JOIN keyword to perform the left join operation on the loan and borrower table where ‘l’ and ‘b’ are aliases. These two tables are joined on the column LoanID which is present in both the tables.

You will get the following output:

LoanIDBranchAmountCustIDCustName
1B1150001Sonakshi Dixit
2B2100004Isha Deshmukh
3B320000NULLNULL
4B41000002Shital Garg
5B51500003Swara Joshi
6B650000NULLNULL
7B7350005Swati Bose
8B885000NULLNULL

LoanID, Branch, Amount, CustID, CustName are retrieved from loan and borrower tables. All the records from the loan table are retrieved. Only those records that have a corresponding LoanID in the loan table are retrieved from the borrower table. Rest other records in the borrower table for which a LoanID doesn’t match with the LoanID of the loan table; then, it is displayed as NULL.

Example 3:

Write a query to perform the left join operation considering the customer table as the left table and the orders table as the right table.

Query:

mysql> SELECT c.Customer_ID, c.Name, c.Age, c.Salary, o.Order_ID, o.Order_Date, o.Amount FROM customer c LEFT JOIN orders o ON c.Customer_ID = o.Customer_ID;  

We have used the SELECT command to retrieve Customer_ID, Name, Age, Salary, Order_ID, Order_Date, Amount present in customer and orders table. Then we have used the LEFT JOIN keyword to perform left join operation on the customer and orders table where ‘c’ and ‘o’ are aliases. These two tables are joined on the column Customer_ID which is present in both the tables.

You will get the following output:

Customer_IDNameAgeSalaryOrder_IDOrder_DateAmount
1Aryan Jain5156000NULLNULLNULL
2Arohi Dixit212500012012-01-203000
2Arohi Dixit212500022012-05-182000
3Vineet Garg243100032012-06-284000
4Ajeet Singh233200042012-04-115000
5Ravi Rathod2342000NULLNULLNULL
6Paras Aggrawal2250000NULLNULLNULL
7Sonakshi Kapadiya2428000NULLNULLNULL
8Sonali Kapoor288200052012-05-048000

Customer_ID, Name, Age, Salary, Order_ID, Order_Date, Amount are retrieved from customer and orders tables. All the records from the customer table are retrieved. Only those records that have a corresponding Customer_ID in the customer table are retrieved from the orders table.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *