Open In App

SQL Full Outer Join Using Where Clause

Improve
Improve
Like Article
Like
Save
Share
Report

A 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 about FULL OUTER JOIN using WHERE clause.

Consider the two tables below:

Sample Input Table 1

                             PURCHASE INFORMATION
Product_ID Mobile_Brand Cost (INR) Customer_Name
1 OnePlus Nord 5G 30,000 Rishabh
2 Samsung Galaxy M51 28,000 Srishti
3 iPhone 12 Pro 1,28,000 Aman
4 Samsung Galaxy S20 55,000 Harsh
5 Realme X50 Pro 40,000 Manjari

Sample Input Table 2 :

                 CUSTOMER INFORMATION
Customer_ID Customer_Name E_Mail Address
1 Srishti abc@gmail.com
2 Rajdeep def@gmail.com
3 Aman xxx@gmail.com
4 Pooja xyz@gmail.com

FULL OUTER JOIN : Full Join provides result with 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 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.

Sample Output :

                                                                                  RESULTS
Product_ID Mobile_Brand Cost (INR) Customer_Name Customer_ID Customer_Name E_Mail Address
1 OnePlus Nord 5G 30,000 Rishabh NULL NULL NULL
2 Samsung Galaxy M51 28,000 Srishti 1 Srishti abc@gmail.com
3 iPhone 12 Pro 1,28,000 Aman 3 Aman xxx@gmail.com
4 Samsung Galaxy S20 55,000 Harsh NULL NULL NULL
5 Realme X50 Pro 40,000 Manjari NULL NULL NULL
NULL NULL NULL NULL 2 Rajdeep def@gmail.com
NULL NULL NULL NULL 4 Pooja xyz@gmail.com

FULL OUTER JOIN using WHERE CLAUSE : The use of WHERE clause with FULL OUTER JOIN helps to retrieve all those rows which have no entry matching on joining both the tables having NULL entry. 

SELECT * FROM Table1
FULL OUTER JOIN Table2
ON Table1.column_match=Table2.column_match
WHERE Table1.column is NULL
OR Table2.column is NULL;

Table1: First Table in Database.
Table2: Second Table in Database.
column_match: The column common to both the tables.
column: The column having NULL value after Full Outer Join

The above Query returns only those customer who bought mobile phones and don’t have any record saved in Customer Information Table as well as the customer information who didn’t buy any product.

SQL QUERY FOR THE SAMPLE INPUTS : We have considered a 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 have information about the Customer. Now, we will perform 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.

BASIC SQL QUERY :

1. Creating a Database

CREATE DATABASE database_name;

2. Creating a Table

CREATE TABLE Table_name(
col_1 TYPE col_1_constraint,
col_2 TYPE col_2 constraint
.....
)

col: Column name
TYPE: Data type whether an integer, variable character, etc
col_constraint: Constraints in SQL like PRIMARY KEY, NOT NULL, UNIQUE, REFERENCES, etc

3. Inserting into a Table

INSERT INTO Table_name
VALUES(val_1, val_2, val_3, ..........)

val: Values in particular column

4. View The Table

SELECT * FROM Table_name

For more information regarding SQL syntaxes visit our website SQL Tutorial.

Output :

1. Customer Information and Purchase Information Table

Purchase Information Table

Customer Information Table

2. Full Outer Join 

Result Table of FULL OUTER JOIN

3. Full Outer Join with WHERE clause

FULL OUTER JOIN  with WHERE clause Resultant Table


Last Updated : 25 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads