Difference between Left, Right and Full Outer Join - GeeksforGeeks

03 May, 2020
Database Management System (DBMS) allows retrieving data from more than one table using joins.
Joins are mainly Cartesian product of two or more relations (or tables).
SQL Joins are broadly categorized as Inner Join and Outer Join. Inner Join selects rows from the tables that fulfills the join condition. But using inner join the data specifically the rows from both the tables that do not satisfy the condition are lost. Outer Join can be used to prevent the loss of data from the tables.
Types of Outer Join :Outer join is again classified into 3 types: Left Outer Join, Right Outer Join, and Full Outer Join. These are explained as following below.
Left Outer Join:Left Outer Join returns all the rows from the table on the left and columns of the table on the right is null padded. Left Outer Join retrieves all the rows from both the tables that satisfy the join condition along with the unmatched rows of the left table.Syntax:SELECT [column1, column2, ....]
FROM   table1

LEFT OUTER JOIN table2 ON 
table1.matching_column = table2.matching_column

WHERE [condition]; OrSELECT [column1, column2, ....]
FROM   table1

LEFT OUTER JOIN table2 
ON table1.matching_column = table2.matching_column

WHERE [condition]; Diagrammatic Representation :Right Outer Join:Right Outer Join returns all the rows from the table on the right and columns of the table on the left is null padded. Right Outer Join retrieves all the rows from both the tables that satisfy the join condition along with the unmatched rows of the right table.Syntax:SELECT [column1, column2, ....]
FROM   table1

RIGHT OUTER JOIN table2 ON 
table1.matching_column = table2.matching_column

WHERE [condition]; Or,SELECT [column1, column2, ....]
FROM   table1

RIGHT OUTER JOIN table2 
ON table1.matching_column(+) = table2.matching_column

WHERE [condition]; Diagrammatic Representation :Full Outer Join:Full Outer Join returns all the rows from both the table. When no matching rows exist for the row in the left table, the columns of the right table are null padded. Similarly, when no matching rows exist for the row in the right table, the columns of the left table are null padded. Full outer join is the union of left outer join and right outer join.Syntax:SELECT [column1, column2, ....]
FROM   table1

FULL OUTER JOIN table2 
ON table1.matching_column = table2.matching_column

WHERE [condition]; Diagrammatic Representation :
Left Outer Join:Left Outer Join returns all the rows from the table on the left and columns of the table on the right is null padded. Left Outer Join retrieves all the rows from both the tables that satisfy the join condition along with the unmatched rows of the left table.Syntax:SELECT [column1, column2, ....]
FROM   table1

LEFT OUTER JOIN table2 ON 
table1.matching_column = table2.matching_column

WHERE [condition]; OrSELECT [column1, column2, ....]
FROM   table1

LEFT OUTER JOIN table2 
ON table1.matching_column = table2.matching_column

WHERE [condition]; Diagrammatic Representation :
Syntax:
SELECT [column1, column2, ....]
FROM   table1

LEFT OUTER JOIN table2 ON 
table1.matching_column = table2.matching_column

WHERE [condition]; 
Or
SELECT [column1, column2, ....]
FROM   table1

LEFT OUTER JOIN table2 
ON table1.matching_column = table2.matching_column

WHERE [condition]; 
Diagrammatic Representation :
Right Outer Join:Right Outer Join returns all the rows from the table on the right and columns of the table on the left is null padded. Right Outer Join retrieves all the rows from both the tables that satisfy the join condition along with the unmatched rows of the right table.Syntax:SELECT [column1, column2, ....]
FROM   table1

RIGHT OUTER JOIN table2 ON 
table1.matching_column = table2.matching_column

WHERE [condition]; Or,SELECT [column1, column2, ....]
FROM   table1

RIGHT OUTER JOIN table2 
ON table1.matching_column(+) = table2.matching_column

WHERE [condition]; Diagrammatic Representation :
Syntax:
SELECT [column1, column2, ....]
FROM   table1

RIGHT OUTER JOIN table2 ON 
table1.matching_column = table2.matching_column

WHERE [condition]; 
Or,
SELECT [column1, column2, ....]
FROM   table1

RIGHT OUTER JOIN table2 
ON table1.matching_column(+) = table2.matching_column

WHERE [condition]; 
Diagrammatic Representation :
Full Outer Join:Full Outer Join returns all the rows from both the table. When no matching rows exist for the row in the left table, the columns of the right table are null padded. Similarly, when no matching rows exist for the row in the right table, the columns of the left table are null padded. Full outer join is the union of left outer join and right outer join.Syntax:SELECT [column1, column2, ....]
FROM   table1

FULL OUTER JOIN table2 
ON table1.matching_column = table2.matching_column

WHERE [condition]; Diagrammatic Representation :
Syntax:
SELECT [column1, column2, ....]
FROM   table1

FULL OUTER JOIN table2 
ON table1.matching_column = table2.matching_column

WHERE [condition]; 
Diagrammatic Representation :
Example:Consider following employee table,
Department Table :
Now,1. Left Outer Join query –
Select empid, ename, deptid, deptname 
from employee 
left outer join department 
on employee.empdept = department.deptname; 
Output:
2. Right Outer Join query –
Select empid, ename, deptid, deptname 
from employee right outer join department 
on employee.empdept = department.deptname;
3. Full Outer Join query –
Select empid, ename, deptid, deptname 
from employee full outer join department 
on employee.empdept = department.deptname;
Differences between Left Outer Join, Right Outer Join, Full Outer Join :
DBMS-Join
Picked
DBMS
Difference Between
GATE CS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction of B-Tree
Difference between Clustered and Non-clustered index
Introduction of DBMS (Database Management System) | Set 1
Introduction of ER Model
CTE in SQL
Difference between BFS and DFS
Class method vs Static method in Python
Difference between var, let and const keywords in JavaScript
Differences between TCP and UDP
Difference between Process and Thread