Skip to content
Related Articles

Related Articles

SQL Full Outer Join Using Left and Right Outer Join and Union Clause
  • Difficulty Level : Medium
  • Last Updated : 21 Apr, 2021

An SQL join statement is used to combine rows or information from two or more than two tables on the basis of a common attribute or field. There are basically four types of JOINS in SQL.

In this article, we will discuss FULL OUTER JOIN using LEFT OUTER Join, RIGHT OUTER JOIN, and UNION clause.

Consider the two tables below:

Sample Input Table 1:

                              Purchase Information
Product_IDMobile_BrandCost (INR)Customer_Name
1OnePlus Nord 5G30,000Rishabh
2Samsung Galaxy M5128,000Srishti
3iPhone 12 Pro1,28,000Aman
4Samsung Galaxy S2055,000Harsh
5Realme X50 Pro40,000Manjari

Sample Input Table 2:



                  Customer Information
Customer_IDCustomer_NameE_Mail Address
1Srishtiabc@gmail.com
2Rajdeepdef@gmail.com
3Amanxxx@gmail.com
4Poojaxyz@gmail.com

FULL OUTER JOIN

Full Join provides results with the concatenation of LEFT JOIN and RIGHT JOIN. The result will contain all the rows from both Table 1 and Table 2. The rows having no matching in the result table will have NULL values.

SELECT * FROM Table1
FULL OUTER JOIN Table2
ON Table1.column_match=Table2.column_match;

Table1: First Table in Database.
Table2: Second Table in Database.
column_match: The column common to both the tables.

The above query can also be written using a combination of LEFT OUTER JOIN, RIGHT OUTER JOIN, and UNION. The meaning of UNION is to join two or more data sets into a single set. The above query and the below query will provide the same output.

SELECT * FROM Table1
LEFT OUTER JOIN Table2
ON Table1.column_match=Table2.column_match
UNION
SELECT * FROM Table1
RIGHT OUTER JOIN Table2
ON Table1.column_match=Table2.column_match;

Table1: First Table in Database.
Table2: Second Table in Database.
column_match: The column common to both the tables.

Sample Output:

                                                                                   RESULTS
Product_IDMobile_BrandCOST (INR)Customer_NameCustomer_IDCustomer_NameE_Mail Address
1OnePlus Nord 5G30,000RishabhNULLNULLNULL
2Samsung Galaxy M5128,000Srishti1Srishtiabc@gmail.com
3iPhone 12 Pro1,28,000Aman3Amanxxx@gmail.com
4Samsung Galaxy S2055,000HarshNULLNULLNULL
5Realme X50 Pro40,000ManjariNULLNULLNULL
NULLNULLNULLNULL2Rajdeepdef@gmail.com
NULLNULLNULLNULL4Poojaxyz@gmail.com

SQL QUERY FOR THE SAMPLE INPUTS: 

We have considered the Customer and Purchase Information of mobile phones from an E-Commerce site during Big Billion Days. The Database E-Commerce has two tables one has information about the Product and the other one has information about the Customer. Now, we will perform a FULL OUTER JOIN between these two tables to concatenate them into a single table and get complete data about the customers and the products they purchased from the site.

Now let’s consider the purchase_information table. To view the table use the below query:

SELECT * FROM purchase_information; 

Output:

Purchase Table

To view the customer_information table use the below query:

SELECT * FROM customer_information;

Output:

Customer Table

Now we can simply call the FULL OUTER JOIN clause to achieve a combined result from both the above-created tables using the below query:

SELECT * FROM purchase_information
FULL OUTER JOIN customer_information
ON purchase_information.cust_name=customer_information.customer_name

Output:

RESULT TABLE USING FULL OUTER JOIN

But we can achieve the same results without using the FULL OUTER JOIN clause. For this we make use of the LEFT JOIN, RIGHT JOIN, and the UNION clause as shown below:

SELECT FROM purchase_information
LEFT OUTER JOIN customer information
ON purchase_information.cust_name=customer_information.customer_name
UNION
SELECT * FROM purchase_information

RIGHT OUTER JOIN customer_information
ON purchase_information.cust_name=customer_information.customer_name

Output:

RESULTANT TABLE OF FULL OUTER JOIN USING LEFT AND RIGHT AND UNION

My Personal Notes arrow_drop_up
Recommended Articles
Page :