There are instances when we want to look at data that matches between two or more tables. There are also instances when we want to return data that does not match. We can look for data that does not exist in the left table, or for data that does not exist in the right table. In this tutorial, we will focus on data that does not match in either table.
Let's come back to our initial dataset with the representative and department tables to help illustrate the outer joins:
CREATE TABLE representative ( representative_id INT PRIMARY KEY, first_name VARCHAR (30) NOT NULL, last_name VARCHAR (30) NOT NULL );
CREATE TABLE department ( department_id INT PRIMARY KEY, department_name VARCHAR (100) NOT NULL, manager_id INT, constraint fk_manager FOREIGN KEY (manager_id) REFERENCES representative (representative_id) );
INSERT INTO representative (representative_id, first_name, last_name)
VALUES (1, 'Bob','Evans'), (2, 'Tango','Rushmore'), (3, 'Danika','Arkane'), (4, 'Mac','Anderson');
INSERT INTO department (department_id, department_name, manager_id)<br>
VALUES (1, 'Sales', 1), (2, 'Marketing', 3), (3, 'IT', 4), (4, 'Finance', null), (5, 'Support', null);
There are multiple types of outer joins, including the full outer join, as well as the left and right outer joins, which we will discuss in forthcoming tutorials.
The full outer join, or full join, returns the data from all rows in both the left and right tables. If they match, it will return data from both sides. If they don't match, the columns of the table will be filled with NULL.
Let's first look at the structure of the statement:
SELECT <columnlist>
FROM <table1>
FULL OUTER JOIN <table2> ON <table1>.<table1column1> = <table2>.<table2column1>;
Notice that this is very similar to a regular JOIN ON statement, with only the FULL OUTER added to the JOIN. Using our two tables, we can run the full outer join like this:
SELECT *
FROM representative
FULL OUTER JOIN department ON representative.representative_id = department.manager_id;
This will return the three records that match the representative_id from the representative table and the department_id from the department table. It will also return the rows from the department table that do not have a matching row in the representative table (4th and 5th row in the result set). It will also return the rows from the representative table that do not have a match in the department table (6th row):
This is an important point. If every row in both tables matches one another, the full outer join would have the same result set.
The following Venn diagram shows what the data would return:
[MUSIC PLAYING] An inner join joins two way tables together and displays a data that matches between two different tables. With outer joins, it does a little bit different. In this case here, when I explore the use of the full outer join, which will display data that matches between two tables, but also, the rows between the two way tables that actually don't match together.
So in this case here, this is a typical inner joint statement utilizing the on clause. The full outer join will just replace inner with full outer. And once we run this, we're going to be presented with the rows that match between the two tables, and then the two rows in this case here within the apartment table that don't have a representative link to it, and also, the representative in this case, here, the single one that doesn't have a department, that's going to be linked.
This case here, it'll provide us with that information of all the data that's not within either one. Typically, you'd utilize the left or the right outer join rather than the full outer join as you want specific data about a specific table. However, the full outer join provides us that information between the two different tables and all those items that don't match up together. This, for example, could be quite useful to be able to identify customers that haven't made any orders within a database, or perhaps the customers that haven't made the orders, as well as the products that have never been purchased.
[MUSIC PLAYING]
Source: Authored by Vincent Tran