Outer joins
An outer join is a method of combining two or more tables so that the result includes unmatched rows of one of the tables, or of both tables. The matching is based on the join condition.
Db2 supports three types of outer joins:
- Left outer join
- A left outer join result includes the rows from the left table that were missing from the inner join. The result of
T1 LEFT OUTER JOIN T2consists of their paired rows and, for each unpaired row of T1, the concatenation of that row with the null row of T2. All columns derived from T2 allow null values. For more information, see Left outer join. - Right outer join
- A right outer join result includes the rows from the right table that were missing from the inner join. The result of
T1 RIGHT OUTER JOIN T2consists of their paired rows and, for each unpaired row of T2, the concatenation of that row with the null row of T1. All columns derived from T1 allow null values. For more information, see Right outer join. - Full outer join
- A full outer join result includes the rows from both tables that were missing from the inner join. The result of
T1 FULL OUTER JOIN T2consists of their paired rows and, for each unpaired row of T1, the concatenation of that row with the null row of T2, and for each unpaired row of T2, the concatenation of that row with the null row in T1. All columns of the result table allow null values. For more information, see Full outer join.
Outer join examples
The following examples use two tables: the parts table (PARTS) and the products table (PRODUCTS), which consist of hardware supplies.
The following figure shows that each row in the PARTS table contains data for a single part: the part name, the part number, and the supplier of the part.
The following figure shows that each row in the PRODUCTS table contains data for a single product: the product number, name, and price.
The following figure shows the ways to combine the PARTS and PRODUCTS tables by using outer join functions. The illustration is based on a subset of columns in each table.
An inner join consists of rows that are formed from the PARTS and PRODUCTS tables, based on matching the equality of column values between the PROD# column in the PARTS table and the PROD# column in the PRODUCTS table. The inner join does not contain any rows that are formed from unmatched columns when the PROD# columns are not equal.
You can specify joins in the FROM clause of a query. Data from the rows that satisfy the search conditions are joined from all the tables to form the result table.
The result columns of a join have names if the outermost SELECT list refers to base columns. However, if you use a function (such as COALESCE) to build a column of the result, that column does not have a name unless you use the AS clause in the SELECT list.