PostgreSQL FULL OUTER JOIN
Summary: in this tutorial, you will learn how to use the PostgreSQL FULL OUTER JOIN
to query data from two tables.
Introduction to the PostgreSQL FULL OUTER JOIN clause
The FULL OUTER JOIN
combine data from two tables and returns all rows from both tables, including matching and non-matching rows from both sides.
In other words, the FULL OUTER JOIN
combines the results of both the left join and the right join.
Here’s the basic syntax of FULL OUTER JOIN
clause:
In this syntax:
- First, specify the columns from
table1
andtable2
in theselect_list
. - Second, specify the
table1
that you want to retrieve data in theFROM
clause. - Third, specify the
table2
that you want to join with thetable1
in theFULL OUTER JOIN
clause. - Finally, define a condition for joining two tables.
The FULL OUTER JOIN
is also known as FULL JOIN
. The OUTER
keyword is optional.
How the FULL OUTER JOIN works
Step 1. Initialize the result set:
- The
FULL OUTER JOIN
starts with an empty result set.
Step 2. Match rows:
- First, identify rows in
table1
andtable2
where the values in the specifiedcolumn_name
match. - Then, include these matching rows in the result set.
Step 3. Include non-matching rows from the table1
and table2
:
- First, include rows from
table1
that do not have a match intable2
. For the columns fromtable2
in these rows, include NULLs. - Second, include rows from
table2
that do not have a match intable1
. For the columns fromtable1
in these rows, include NULLs.
Step 4. Return the result set:
- Return the final result set will contain all rows from both tables, with matching rows and non-matching rows from both
table1
andtable2
. - If a row has a match on both sides, combine the values into a single row.
- If there is no match on one side, the columns from the non-matching side will have NULLs.
The following Venn diagram illustrates the FULL OUTER JOIN
operation:
Setting up sample tables
First, create two new tables for the demonstration: employees
and departments
:
Each department has zero or many employees and each employee belongs to zero or one department.
Second, insert some sample data into the departments
and employees
tables.
Third, query data from the departments
and employees
tables:
Output:
Output:
PostgreSQL FULL OUTER JOIN examples
Let’s take some examples of using the FULL OUTER JOIN
clause.
1) Basic FULL OUTER JOIN examaple
The following query uses the FULL OUTER JOIN
to query data from both employees
and departments
tables:
Output:
The result set includes every employee who belongs to a department and every department which have an employee.
Additionally, it includes every employee who does not belong to a department and every department that does not have an employee.
2) Using FULL OUTER JOIN with WHERE clause example
The following example use the FULL OUTER JOIN
with a WHERE clause to find the department that does not have any employees:
Output:
The result shows that the Production
department does not have any employees.
The following example use the FULL OUTER JOIN
cluase with a WHERE
clause to find employees who do not belong to any department:
Output:
The output shows that Juila Mcqueen
does not belong to any department.
Summary
- Use the PostgreSQL
FULL OUTER JOIN
clause to combine data from both tables, ensuring that matching rows are included from both the left and right tables, as well as unmatched rows from either table.