PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Tutorial / PostgreSQL FULL OUTER JOIN

PostgreSQL FULL OUTER JOIN

Summary: in this tutorial, you will learn how to use the PostgreSQL FULL OUTER JOIN to query data from two or more tables.

Introduction to the PostgreSQL FULL OUTER JOIN

Suppose, you want to perform a full outer join of two tables: A and B. The following illustrates the syntax of the FULL OUTER JOIN:

1
2
SELECT * FROM A
FULL [OUTER] JOIN B on A.id = B.id;

The OUTER keyword is optional.

The full outer join combines the results of both left  join and right join. If the rows in the joined table do not match, the full outer join sets NULL values for every column of the table that lacks a matching row. For the matching rows , a single row is included in the result set that contains columns populated from both joined tables.

The following Venn diagram illustrates the FULL OUTER JOIN operation:

PostgreSQL full outer join

The result includes the matching rows from the both tables, and also the rows that do not match.

PostgreSQL FULL OUTER JOIN example

First, we will create two new tables for the demonstration: employees and departments:

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE
IF NOT EXISTS departments (
department_id serial PRIMARY KEY,
department_name VARCHAR (255) NOT NULL
);
 
CREATE TABLE
IF NOT EXISTS employees (
employee_id serial PRIMARY KEY,
employee_name VARCHAR (255),
department_id INTEGER
);

Each department has zero or many employees and each employee belongs to zero or one department.

The following INSERT statements add some sample data into the departments and employees tables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
INSERT INTO departments (department_name)
VALUES
('Sales'),
('Marketing'),
('HR'),
('IT'),
('Production');
 
INSERT INTO employees (
employee_name,
department_id
)
VALUES
('Bette Nicholson', 1),
('Christian Gable', 1),
('Joe Swank', 2),
('Fred Costner', 3),
('Sandra Kilmer', 4),
('Julia Mcqueen', NULL);

Next, we query data from the departments and employees tables:

1
2
3
4
5
6
7
8
9
# SELECT * FROM departments;
department_id | department_name
---------------+-----------------
             1 | Sales
             2 | Marketing
             3 | HR
             4 | IT
             5 | Production
(5 rows)

1
2
3
4
5
6
7
8
9
10
# SELECT * FROM employees;
employee_id |  employee_name  | department_id
-------------+-----------------+---------------
           1 | Bette Nicholson |             1
           2 | Christian Gable |             1
           3 | Joe Swank       |             2
           4 | Fred Costner    |             3
           5 | Sandra Kilmer   |             4
           6 | Julia Mcqueen   |
(6 rows)

Then, we use the FULL OUTER JOIN to query data from both employees and departments tables.

1
2
3
4
5
6
SELECT
employee_name,
department_name
FROM
employees e
FULL OUTER JOIN departments d ON d.department_id = e.department_id;

The result set includes every employee who belongs to a department and every department which have an employee. In addition, it includes every employee who does not belong to a department and every department that doesn’t have an employee.

1
2
3
4
5
6
7
8
9
10
  employee_name  | department_name
-----------------+-----------------
Bette Nicholson | Sales
Christian Gable | Sales
Joe Swank       | Marketing
Fred Costner    | HR
Sandra Kilmer   | IT
Julia Mcqueen   | NULL
NULL            | Production
(7 rows)

To find the department that does have any employee, we use a WHERE clause as follows:

1
2
3
4
5
6
7
8
SELECT
employee_name,
department_name
FROM
employees e
FULL OUTER JOIN departments d ON d.department_id = e.department_id
WHERE
employee_name IS NULL;

1
2
3
4
employee_name | department_name
---------------+-----------------
NULL          | Production
(1 row)

The result shows that the Production department does not have any employee.

To find the employee who does not belong to any department, we check for the NULL of the department_name in the WHERE clause as the following statement:

1
2
3
4
5
6
7
8
SELECT
employee_name,
department_name
FROM
employees e
FULL OUTER JOIN departments d ON d.department_id = e.department_id
WHERE
department_name IS NULL;

1
2
3
4
employee_name | department_name
---------------+-----------------
Julia Mcqueen | NULL
(1 row)

As you see,  Juila Mcqueen does not belong to any department.

In this tutorial, you have learned how to use the PostgreSQL FULL OUTER JOIN clause to join two or more tables.

Related Tutorials

  • PostgreSQL INNER JOIN
  • PostgreSQL LEFT JOIN
  • PostgreSQL Cross Join By Example
  • PostgreSQL NATURAL JOIN Explained By Examples
Previous Tutorial: PostgreSQL INNER JOIN
Next Tutorial: PostgreSQL LEFT JOIN

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.