DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Basics » Db2 FULL OUTER JOIN

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 ?
  • YesNo
Previous Db2 RIGHT JOIN
Next Db2 Self-Join

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.