SQL OUTER JOIN

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

 Inner, Left, Right and Outer joins

Overview

An outer join is a type of join operation in SQL that allows you to combine rows from two or more tables, even if there are no matching rows in one or more of the tables. In an outer join, all rows from one table (the "outer" table) are included in the result set, along with matching rows from the other table(s), if any exist.

Scope

In this article, we will learn about the following:

  • What is outer join in SQL?
  • Types of outer join in SQL, such as left outer join, right outer join and full outer join.
  • Why and when to use an outer join, along with examples.

What is a OUTER JOIN in SQL?

We use the normalization process to organize the data and remove the redundancy from the relational database. Normalization is a process of dividing the relation (table) into multiple small relations to eliminate data repetition and inconsistency (errors).To retrieve the original relation from normalized relations, we use the select statement in Joins.

SQL Outer Join returns all the rows of the participating tables that satisfy the join conditions and rows not satisfying the join conditions.

The outer join in SQL is denoted by the (+)(+) operator only on one side of the join condition. Outer join in SQL is of three types.

  1. Left Outer Join
  2. Right Outer Join
  3. Full Outer Join

Syntax of OUTER JOIN in SQL

The syntax of the outer join operation is as follows:

We select columns from the tableA that are to be part of the outer join. Next, we mention FULL OUTER JOIN and tableB. ON defines common criteria for the join; say, one column is common in both the relations, and join is performed on it. Also, we can mention the WHERE condition if we wish to filter some records.

The outer join of two tables tableA and tableB can be represented as follows:

Outer joint in SQL

Note: The values common in tableA and tableB appear only once in the outer join.

Example of OUTER JOIN in SQL

Let us take an example of the Student relation and Course relation. The student table consists of the student name and id and the course table consists of the course id, course name, and students enrolled in it.

Student Table

Student_IDStudent_Name
3001Albert
3002Maria
3005Darwin
3007Elizabeth
3008Patrik

Course Table

Course_IDStudent_IDCourse
113002Java
123009C++
133005Python
153001DBMS
103002Scala

Now let us perform the outer join.

Output:

Student_NameStudent_IDCourse_IDCourse
Maria300211Java
NULL300912C++
Darwin300513Python
Albert300115DBMS
Maria300210Scala
Patrik3008NULLNULL
Elizabeth3007NULLNULL

The above table is the result of the outer join of the Student and Course tables. We can observe that

  • for the course_ID 12, as there are no entries in the Student table, its columns are null.
  • for the student_ID 3008 and 3007, as there are no data in the course table, columns of the course table are null.
  • for the student_id 3002, as there are two courses it appears twice.

Types of OUTER JOIN in SQL

LEFT OUTER JOIN in SQL

Left outer join operation in SQL returns all the rows from the left, i.e., first relation, and only matching rows from the right, i.e., second relation. In case some row is present in the left table that is absent in the right table, then values of rows of the right table are set to zero, which can later be modified.

Visualization of Left outer join with the help of Venn diagram. The resultant relation consists of all the rows of the left table, i.e., tableA, and only matching rows of the right table.

Left Outer Joint

Syntax of LEFT OUTER JOIN

The syntax of left join is the same as full join, except there will be a LEFT keyword instead of FULL.

Left join keywords are used to perform left outer join. ON is used to identify the columns in each table to be linked.

LEFT OUTER JOIN Example

Let us take an example of a shopping list and a list of stores in the city to understand left outer join in SQL.

Item_list table consists of a list of items, quantities, and categories. Store_list consists of a list of stores, their id, and location. We will perform left join with item_list table as our left table as we want all the items of the list but only specific stores.

Table Item_list

ItemQuantityCategory
Chips2Grocery
Washing Powder1Grocery
Slippers1Footwear
Nailcutter1Medical
Rubber Bands5Stationary

Table Store_list

Store_IDLocationCategory
7922Iconic StreetMedical
4773Diagon AlleyStationary
793919 Badfort StreetFootwear
0211Aweer CentralGrocery
8392New Lane 22Greengrocers

Output:

ItemQuantityCategoryStore_IDLocation
Nailcutter1Medical7922Iconic Street
Rubber Band5Stationary4773Diagonal Alley
Slippers1Footwear793919 Badfort Street
Chips2Grocery211Aweer Central
Washing Powder1Grocery211Aweer Central

As in the above example, all the rows of the item_list table, i.e., left table, were included but only matching rows of store_list are seen. As for each item in item_list, an entry exists in store_list. Therefore none of the columns are assigned the NULL value.

RIGHT OUTER JOIN in SQL

The right outer join operation in SQL returns all the rows from the right, i.e., the second relation and only matching rows from the left relation. If some records from the right table don't match rows from the left table, the null value is inserted in the columns of the left table. The records that don't match in the left table are discarded.

The Right outer join can be visualized as follows using a Venn diagram. As all the records(rows) of relation are present at the right and matching records on the left, i.e., tableA, are present in the resultant relation.

Right Outer joint

Syntax of RIGHT OUTER JOIN

The syntax of Right Outer Join is the same as Left outer join except for the LEFT keyword; we use the RIGHT keyword. Here all the right table rows are included but only matching rows of the left table are added.

RIGHT OUTER JOIN Example

Let's say we have two relations, the first for the customer and the other for the order of customers. Some customers are regular and thus have their data in the record, i.e., customer_id to uniquely identify them, customer_name, and their order_id.

The other relation, order_table, has a list of orders along with order_id, order_name, and order_status. It is possible that the customer is not registered on the customer table, but still, their order exists.

Table Customer

Customer_IDCustomer_NameOrder_ID
611SapnaORD-556
672MaheshORD-879
651RamnathORD-321
609RohanORD-901
601JayORD-430

Table Order_table

Order_IDOrder_NameOrder_Status
ORD-901PizzaOn the way
ORD-879ManchurianIn Process
ORD-430PastaIn Process
ORD-671DhosaOn the way
ORD-012Punjabi ThaliDelivered

Output:

Customer_IDCustomer_IDOrder_IDOrder_NameOrder_Status
672MaheshORD-879ManchurianIn Process
609RohanORD-901PizzaOn the way
601JayORD-430PastaIn Process
NULLNULLORD-671DhosaOn the way
NULLNULLORD-012Punjabi ThaliDelivered

In the above relation, all the orders are included. The details of customers are fetched if it is available in the customer table; else, the columns are kept NULL (in rows 4 and 5). As we perform a right outer join, the customers not having orders are excluded, and customer details are included if order_id matches two tables.

FULL OUTER JOIN in SQL

The full outer join operation returns all the records from both the relations, irrespective of the match from tableA or tableB.

The full outer join is shown below using a Venn diagram. As all the records of both the relations are present in the resultant table, it can be shown using the union of tableA and tableB.

Full outer joint

Syntax of FULL OUTER JOIN

Here we use the FULL keyword to perform a full outer join of two tables.

FULL OUTER JOIN Example

To see how full join works let us take an example of an employee table. Employee table consists of employee_id, emp_name, their designation, and city. Another table called department table contains department details such as dept_id, dept_name, and emp_id.

New employees may not be assigned their departments but are added to the employee table. Also, some employees being part of multiple departments, do not have their names in a specific department. Let us perform a full join to get details of all the employees.

Table Employee

Emp_IDEmp_NameDesignationCity
1Riley WardAssistantDallas
2Sadie GraySenior AnalystNew York
3Emma PhilipsConsultantChicago
4Alyssa LeeLawyerPhoenix
5Taylor GeorgeManagerChicago

Table Department

Dept_IDDept_NameEmp_ID
91Finance2
93Engineering3
92Maintenance1
94Accounting6
95Finance4

Output:

EMP_IDEMP_NAMEDEPT_IDDESIGNATIONCITYDEPT_NAME
2Sadie GraySenior AnalystNew York91Finance
3Emma PhilipsConsultantChicago93Engineering
1Riley WardAssistantDallas92Maintenance
NULLNULLNULLNULL94Accounting
4Alyssa LeeLawyerPhoenix95Finance
5Taylor GeorgeManagerChicagoNULLNULL

The above resultant relation includes all the matching rows from both tables. NULL value is added in the place where entries are not available in either of the relations (tables). For the 4th4^{th} row, we don't have an entry in the employee table; thus, the columns are assigned NULL values. Similarly, in the 6th6^{th} row, we don't have data in the department table. Unlike left or right outer joins, none of the tables are prioritized.

Why Use an OUTER JOIN in SQL?

There are two types of joins in SQL: inner join and outer join.

Inner join returns the combinations of the rows that satisfy the join conditions, and the rest are discarded. Whereas outer join returns the result containing the combinations of rows that satisfy the given condition as well as rows of the dominant table that were discarded in inner join if no matching entries were found in the subordinate table. In the case of non-matching entries in the subordinate table, the resultant table will contain NULL values in columns selected from the subordinate table.

Why to use outer joint

Learn More

Learn more about join in SQL.

Conclusion

  • To normalize the data, we divide it into multiple small relations. To obtain the original relation (table), we use joins as per the requirements.
  • Outer joins are denoted by the (+)(+) operator and are of three types, i.e., left, right, and full outer joins.
  • Left outer join returns all the rows of the left table and only matches rows of the right table. Similarly, the Right outer join returns all the right table rows. Full join returns all the matching rows of both tables.