Summary: in this tutorial, you will learn how to use the Oracle FULL OUTER JOIN
to query data from multiple tables.
Introduction to Oracle FULL OUTER JOIN clause
Suppose you have two tables T1 and T2. The following illustrates the full outer join of the two tables:
SELECT
select_list
FROM
T1
FULL OUTER JOIN T2 ON join_condition;
Code language: SQL (Structured Query Language) (sql)
For each row in the T1 table, the full outer join compares it with every row in the T2 table.
If rows from both tables meet the join_condition
, the full outer join includes columns of both rows in the result set. We say that the row in T1 table matches with the row in the T2 table in this case.
If a row from the T1 table does not have any matching row from the T2 table, the full outer join will include columns from the T1 table with null values for all columns from the T2 table.
Similarly, if a row in the T2 table does not have a matching row in the T1 table, the full outer join will include columns from the T2 table with the null values for the columns in the T1 table.
Note that the OUTER
keyword is optional, therefore, the FULL OUTER JOIN
and FULL JOIN
are the same.
This Venn diagram illustrates the full outer join of two tables:
Oracle FULL OUTER JOIN examples
First, create two tables members
and projects
. Suppose each member can join zero or one project, and each project can have zero or more members:
CREATE TABLE projects(
project_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
project_name VARCHAR2(100) NOT NULL
);
CREATE TABLE members(
member_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
member_name VARCHAR2(100) NOT NULL,
project_id INT,
FOREIGN KEY (project_id) REFERENCES projects(project_id)
);
Code language: SQL (Structured Query Language) (sql)
Second, insert some projects and members into the projects
and members
tables:
INSERT INTO projects(project_name)
VALUES('ERP');
INSERT INTO projects(project_name)
VALUES('Sales CRM');
INSERT INTO members(member_name, project_id)
VALUES('John Doe',1);
INSERT INTO members(member_name, project_id)
VALUES ('Jane Doe',1);
INSERT INTO members(member_name, project_id)
VALUES ('Jack Daniel',null);
Code language: SQL (Structured Query Language) (sql)
Third, use the full outer join to query data from members
and projects
tables:
SELECT
member_name,
project_name
FROM
members m
FULL OUTER JOIN projects p ON p.project_id = m.project_id
ORDER BY
member_name;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
Jack Daniel does not join any project, Jane Doe and John Doe join the ERP project, and the Sales CRM project has no members.
To find the project that does not have any member, you use the following query:
SELECT
project_name,
member_name
FROM
members m
FULL OUTER JOIN projects p
ON p.project_id = m.project_id
WHERE
member_name IS NULL
ORDER BY
member_name;
Code language: SQL (Structured Query Language) (sql)
Similarly, you can find members who do not participate in any project by using the following query:
SELECT
member_name,
project_name
FROM
members m
FULL OUTER JOIN projects p
ON p.project_id = m.project_id
WHERE
project_name IS NULL
ORDER BY
member_name;
Code language: SQL (Structured Query Language) (sql)
In this tutorial, you have learned how to use the Oracle FULL OUTER JOIN
clause to query data from multiple tables.