SQL FULL OUTER JOIN

This article will provide you a clear explanation of the FULL OUTER JOIN operator with the help of simple and suitable examples. Before moving on to the main topic first, let us understand about FULL OUTER JOIN operator briefly.

What is FULL OUTER JOIN operator?

Full outer join is a type of join operation that merges the result sets of two tables, including both matching and non-matching rows from both tables. When both tables match the requested join condition, all rows from both tables are returned, and the columns from the non-matching table are filled up with NULL values if there is no match between the tables. The functioning of the full outer join operation is mentioned below

SQL FULL OUTER JOIN

Syntax:

The basic syntax of full outer join operation is mentioned and each term in it is explained below

Here table 1 and table 2 are the two tables which are need to joined based on a specific condition.

Matching rows: The columns from both tables are included in the result set when a row from table1 matches a row from table2 according to the join condition.

Non-matching rows from table 1: If there isn't a match for a row in table 1, NULL values are utilised to fill in the columns from table 2.

Non-matching rows from table 2: If there isn't a match for a row in table 2, NULL values are utilised to fill in the columns from table 1.

Let's see few examples to understand clearly the working of full outer join operation.

Example 1:

Consider two tables Student, Department tables for demonstrating the functioning of Full Outer Join operator. First let us create these two tables and insert values into them.

Now, we are going to insert values into this table.

The Student table will look like:


IDNameTotal_marksContact_no
1Ram5009127563212
2Joseph4768123456782
3Rehman3899814569871
4Priyanka4507891234561
5Saleem5347765123456
6Dhanush5508877123590

Now, we will create Department table.

Now, we are going to insert values into it.

The Department table will look like:


IDdepartment_namedepartment_id
2CSE122
3ECE123
4IT126

Let's join these two tables using Full Outer Join operator.

Output:

IDNameTotal_marksdepartment_name
1Ram500NULL
2Joseph476CSE
3Rehman389ECE
4Priyanka450IT
5Priyanka450NULL
6Saleem534NULL

Example 2:

Consider below two tables Student and Student Joining.

Roll_nonamedepartment
134AnishCSE
135RahulECE
136SmithaIT
137GaneshMechanical
138ArunEEE

Table: Student

Roll_noAdmission_nojoining_date
134AS12323-11-2020
136AS12621-08-2021
137AS12721-08-2021

Table: Student_joining

Now, we will join these two tables using Full Outer join operator.

Output:

Roll_nonameAdmission_noJoining_date
134AnishAS12323-11-2020
135RahulNULLNULL
136SmithaAS12621-08-2021
137GaneshAS12721-08-2021
138ArunNULLNULL

Example 3:

Let us suppose there are 2 tables product and product_category.

product_idproduct_namecategory_id
1Laptop22
2Shirt24
3Jeans24
4Mobile22
5Book25

Table: product

product_idcategory
2Clothing
5Stationary

Table: product_category

Let us combine these two tables using FULL OUTER JOIN operator on basis of product_id.

The result set will be

Output:

product_idproduct_namecategory
1LaptopNULL
2ShirtClothing
3JeansNULL
4MobileNULL
5BookStationary

FULL OUTER JOIN With WHERE Clause

In the above examples we have joined two tables by using ON clause only, but the records tables can be filtered with the help of WHERE clause i.e, FULL OUTER JOIN can be used along with WHERE Clause to join data based on a specific condition.

Syntax for FULL OUTER JOIN operator along with WHERE clause is provided below:

Observe below example to clear understand how tables can be joined on a specific condition by using WHERE clause along with FULL OUTER JOIN operator.

Example 4:

Consider below two tables employee and department to understand how two tables can be joined based on a specific condition.

emp_idemp_namesalary
45Ramesh22000
46Rahim20000
47Sruthi30000
48Swetha35000
50Priyanka50000
51Anjali40000

Table: employee

emp_iddepartment_namedepartment_id
45HR33
47Development35
48marketing36
51R&D39

Table: department

Now, we are going to execute below command to join above two tables based on condition where salary of employee should be greater than 25000.

The result set will look like

Output:

emp_idemp_namesalarydepartment_name
47Sruthi30000Development
48Swetha35000marketing
51Anjali40000R&D

So, this all about the working of FULL OUTER JOIN operation. This article has provided clear explanation of FULL OUTER JOIN operator with valid and suitable examples.


Next TopicSQL NOT IN