Db2 FULL OUTER JOIN

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

Introduction to Db2 FULL OUTER JOIN clause

Suppose you have two tables named T1 and T2, which are called the left table and the right table respectively.

The FULL OUTER JOIN of the T1 and T2 returns a result set which includes rows from both left and right tables. When no matching rows exist for the row from the left table, the columns of the right table are filled with NULL. Similarly, when no matching rows exist for the row from the right table, the columns of the left table will be filled with NULL.

The following shows the syntax of the FULL OUTER JOIN when joining two tables:

SELECT select_list FROM T1 FULL OUTER JOIN T2 ON join_condition;
Code language: SQL (Structured Query Language) (sql)

The OUTER keyword is optional so you can omit it as shown in the following query:

SELECT select_list FROM T1 FULL JOIN T2 ON join_condition;
Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • First, specify the left table T1 in the FROM clause.
  • Second, specify the right table T2 and a join condition.

This Venn diagram illustrates the FULL OUTER JOIN of two result sets T1 and T2:

db2 full outer join

Db2 FULL OUTER JOIN examples

Let’s take some examples of using the FULL OUTER JOIN clause.

1) Using Db2 FULL OUTER JOIN to join two tables example

This database diagram shows the books and publishers tables:

In this diagram, a publisher may have zero or many books while a book may belong to zero or one publisher. The relationship between the books table and the publishers table is zero-to-many. The publisher_id column in both tables links a book to a publisher and vice versa.

Note that in case a book does not associate with any publisher, maybe the publisher was unknown at the time of inserting, the value in the publisher_id column of the books table is NULL.

The following query uses the FULL OUTER JOIN clause to join the books table with the publishers table:

SELECT b.title, p.name AS publisher FROM books b FULL OUTER JOIN publishers p ON p.publisher_id = b.publisher_id ORDER BY b.title NULLS FIRST, publisher;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

db2 full outer join example

In this example, the query returned books with publishers, books who do not have publishers, and publishers that do have any books.

2) Using Db2 FULL OUTER JOIN to find the missing rows example

To find the publishers who do not have any books and books which do not associate with any publisher, you add a WHERE clause to check if the book title OR publisher name IS NULL:

SELECT b.title, p.name AS publisher FROM books b FULL OUTER JOIN publishers p ON p.publisher_id = b.publisher_id WHERE b.title IS NULL OR p.name IS NULL ORDER BY b.title NULLS FIRST, publisher;
Code language: SQL (Structured Query Language) (sql)

This picture shows the partial output:

db2 full outer join find missing rows

In this tutorial, you have learned how to use the Db2 FULL OUTER JOIN to query data from two tables.

Was this tutorial helpful ?