OUTER JOIN

  • In the SQL outer JOIN, all the content from both the tables is integrated together.
  • Even though the records from both the tables are matched or not, the matching and non-matching records from both the tables will be considered an output of the outer join in SQL.
  • There are three different types of outer join in SQL:
    1. Left Outer Join
    2. Right Outer Join
    3. Full Outer Join

Now let us take a deeper dive into the different types of outer 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

1. Left Outer Join:

  • If we use the left outer 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.
  • Syntax of writing a query to perform left outer join:

SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1 LEFT OUTER JOIN TableName2 ON TableName1.ColumnName = TableName2.ColumnName;

Example 1:

Write a query to perform left outer join considering employee table as the left table and 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 OUTER 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 OUTER JOIN keyword to perform the left outer 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, Department_ID, 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 left outer join considering loan table as the left table and borrower table as the right table.

Query:

mysql> SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l LEFT OUTER 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 OUTER JOIN keyword to perform the left outer 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; are displayed as NULL.

2. Right Outer Join:

  • Right outer join is the reverse of left outer join. If we use the right outer join to combine two different tables, then we will get all the records from the right table. But we will get only those records from the left table, which have the corresponding key in the right table.
  • Syntax of writing a query to perform right outer join:
SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1  RIGHT OUTER JOIN TableName2  ON TableName1.ColumnName = TableName2.ColumnName;  

Example 1:

Write a query to perform right outer join considering employee table as the left table and 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 RIGHT OUTER 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 RIGHT OUTER JOIN keyword to perform the right outer 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
3Harshal Pathak480002Sales
4Arjun Kuwar460003Marketing
5Sarthak Gada620004Accounts
7Shubham Singh290005Development
9Vicky Gujral390006HR
10Vijay Bose280007Sales

EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name are retrieved from employee and department tables. All the records from the department table are retrieved. Only those records that have a corresponding EmployeeID in the department table are retrieved from the employee table.

Example 2:

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

Query:

mysql> SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l RIGHT OUTER 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 RIGHT OUTER JOIN keyword to perform the right outer 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
4B41000002Shital Garg
5B51500003Swara Joshi
2B2100004Isha Deshmukh
7B7350005Swati Bose
NULLNULLNULL6Asha Kapoor
NULLNULLNULL7Nandini Shah

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

3. Full Outer Join:

  • If we use a full outer join to combine two different tables, then we will get all the records from both the table,e., we will get all the records from the left table as well as the right table.
  • MySQL doesn’t support FULL OUTER JOIN directly. So to implement full outer join in MySQL, we will execute two queries in a single query. The first query will be of LEFT OUTER JOIN, and the second query will be of RIGHT OUTER JOIN. We will combine the first and second query with the UNION operator to see the results of FULL OUTER JOIN.
  • Syntax of writing a query to perform full outer join:
SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1  LEFT OUTER JOIN TableName2  ON TableName1.ColumnName = TableName2.ColumnName UNION SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1  RIGHT OUTER JOIN TableName2  ON TableName1.ColumnName = TableName2.ColumnName;  

Example 1:

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

Query:

mysql> SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name FROM department d LEFT OUTER JOIN employee e ON e.EmployeeID = d.Employee_ID UNION SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name FROM department d RIGHT OUTER JOIN employee e 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 OUTER JOIN keyword to perform the left outer join operation on the employee and department table where ‘e’ and ‘d’ are aliases. Then we have written a SELECT query to perform right outer join operation on 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. Both the SELECT queries are combined using the UNION operator.

You will get the following output:

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

EmployeeID, Employee_Name, Employee_Salary, Department_ID, Department_Name are retrieved from employee and department tables. All the records from the employee table are retrieved as a result of the left outer join. 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, are displayed as NULL. All the records from the department table are retrieved as a result of the right outer join. Only those records that have a corresponding EmployeeID in the department table are retrieved from the employee table.

Example 2:

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

Query:

mysql> SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l LEFT OUTER JOIN Borrower b ON l.LoanID = b.LoanID UNION SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l RIGHT OUTER 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 OUTER JOIN keyword to perform the left outer join operation on the loan and borrower table where ‘l’ and ‘b’ are aliases. Then we have written a SELECT query to perform the right outer 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. Both the SELECT queries are combined using the UNION operator.

You will get the following output:

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

LoanID, Branch, Amount, CustID, CustName are retrieved from loan and borrower tables. All the records from the loan table are retrieved as a result of the left outer join. 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; are displayed as NULL. All the records from the borrower table are retrieved as a result of the right outer join. Only those records that have a corresponding LoanID in the borrower table are retrieved from the loan table. Rest other records in the loan table for which a LoanID doesn’t match with the LoanID of the borrower table, are displayed as NULL.


Comments

Leave a Reply

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