joined-table

A joined table specifies an intermediate result table that is the result of either an inner join or an outer join. The table is derived by applying one of the join operators: CROSS, INNER, LEFT OUTER, RIGHT OUTER, or FULL OUTER to its operands.

Read syntax diagramSkip visual syntax diagramtable-referenceINNERouterJOINtable-referenceON  join-conditionUSING(column-list)table-referenceCROSS JOINtable-reference(joined-table)
outer
Read syntax diagramSkip visual syntax diagram LEFTRIGHTFULL OUTER
column-list
Read syntax diagramSkip visual syntax diagram ,column-name
There are several types of joins:
Cross join
Represents the cross product of the tables, where each row of the left table is combined with every row of the right table.
Inner join
Keeps only the rows for which the join condition is true. Rows from either of the joined tables for which the join condition is false are excluded from the result table.
Outer join
Contains the rows for which the join condition is true, plus additional rows:
Left outer join
Also includes rows from the left table for which the join condition is false.
Right outer join
Also includes rows from the right table for which the join condition is false.
Full outer join
Also includes rows from both the left and right tables for which the join condition is false.
The order in which multiple joins are performed can affect the result. Joins can be nested within other joins. The order of processing for joins is generally from left to right, but based on the position of the required join-condition. You can use parentheses to clarify the order of nested joins. For example, the following two statements are equivalent:
   T1 LEFT JOIN T2 ON T1.C1=T2.C1
     RIGHT JOIN T3 LEFT JOIN T4 ON T3.C1=T4.C1
       ON T1.C1=T3.C1
   (T1 LEFT JOIN T2 ON T1.C1=T2.C1)
     RIGHT JOIN (T3 LEFT JOIN T4 ON T3.C1=T4.C1)
       ON T1.C1=T3.C1

A joined table can be used in any context in which any form of the SELECT statement is used. A view or a cursor is read-only if its SELECT statement includes a joined table.

Column references are resolved using the rules for resolution of column name qualifiers. The same rules that apply to predicates apply to join conditions.

Join operations

When a row of T1 is joined with a row of T2, a row in the result consists of the values of that row of T1 concatenated with the values of that row of T2. A join operation might result in the generation of a null row. A null row consists of a null value for each column of a table, regardless of whether null values are allowed in the columns.

The following list summarizes the result of the join operations:
  • The result of T1 CROSS JOIN T2 consists of all possible pairings of their rows.
  • The result of T1 INNER JOIN T2 consists of their paired rows where the join-condition is true.
  • The result of T1 LEFT OUTER JOIN T2 consists of their paired rows where the join-condition is true and, for each unpaired row of T1, the concatenation of that row with the null row of T2. Null values are allowed in all columns derived from T2.
  • The result of T1 RIGHT OUTER JOIN T2 consists of their paired rows where the join-condition is true and, for each unpaired row of T2, the concatenation of that row with the null row of T1. Null values are allowed in all columns derived from T1.
  • The result of T1 FULL OUTER JOIN T2 consists of their paired rows and, for each unpaired row of T2, the concatenation of that row with the null row of T1 and, for each unpaired row of T1, the concatenation of that row with the null row of T2. Null values are allowed in all columns derived from T1 and T2.

Join conditions

A join condition is a qualification expression that involves the two tables to be joined. It specifies pairings of t1 and t2, where t1 and t2 represent the names of the left (t1) and right (t2) operand tables of the JOIN operator. For all possible combinations of rows of t1 and t2, a row of t1 is paired with a row of t2 if the join-condition is true.

A join condition is similar to a search condition, except that:
  • It cannot include any dereference operations or the DEREF function, where the reference value is other than the object identifier column
  • Any column referenced in an expression of the join-condition must be a column of one of the operand tables of the associated join (in the scope of the same joined-table clause)
  • Any function referenced in an expression of the join-condition of a full outer join must be deterministic and have no external action
  • It cannot include an XMLQUERY or XMLEXISTS expression
A join condition must comply with these rules (SQLSTATE 42972).

Join result

You influence which columns are included in the join result by specifying one of the following clauses:
ON <join-condition>
This clause can specify any qualification expression that involves the two tables that are to be joined. For example:
SELECT * FROM t1 JOIN t2 ON t1.c1 = t2.c1 AND t1.c2 = t2.c2;
The join result contains all columns of t1 followed by all columns of t2.
USING <column-list>
This clause joins the tables on the specified columns. Each column exists in both of the tables to be joined. The tables are joined where the value in a column is the same in both tables. For example:
SELECT * FROM t1 JOIN t2 USING (c1, c2);
The columns of the join result depend on the join type:
For an inner join or left outer join
The join result contains the join columns from the t1, followed by the non-join columns from t1, followed by the non-join columns from t2.
For a right outer join
The join result contains the join columns from t2, followed by the non-join columns from t1, followed by the non-join columns from t2.
For a full outer join
The join result has a non-null value from the join columns followed by the non-join columns from t1, followed by the non-join columns from t2.
Note that the join columns appear in the join result only once.
Unless a projection column list is explicitly specified, the order of the columns in the output is the same as in the join result.
Any subsequent, unqualified reference to a join column of a USING clause by another clause (such as a WHERE, ON, ORDER BY, GROUP BY, or HAVING clause) resolves to the column from the join result. For example:
CREATE TABLE t1 (c1 int, c2 varchar(10), c3 numeric(4,2));
CREATE TABLE t2 (c1 bigint, c2 char(8), c4 numeric(6,3));
CREATE TABLE t3 (c3 bigint, c5 int, c6 numeric(6,3));
SELECT * FROM t1 FULL JOIN t2 USING (c1, c2) JOIN t3 ON (c1 = t3.c3);
Column projections: 
  CASE WHEN (t1.c1 IS NOT NULL) THEN t1.c1 ELSE t2.c1 END AS c1
  CASE WHEN (t1.c2 IS NOT NULL) THEN t1.c2 ELSE t2.c2 END AS c2
  t1.c3
  t2.c4
  t3.c3
  t3.c5
  t3.c6
The reference to column c1 in the ON clause resolves to the CASE expression that represents the join column c1 from the full outer join. So, the ON clause is transformed to:
ON ((CASE WHEN (t1.c1 IS NOT NULL) THEN t1.c1 ELSE t2.c1 END) = t3.c3)
The following restrictions apply:
  • The ON and USING clauses are mutually exclusive for a particular join operation, that is, only one of these clauses can be specified when joining two tables. However, a single SQL statement can contain several join operations, and each of these can use either clause.
  • An ON or USING clause cannot be used if a plus symbol (+) is used as the outer join operator.

Examples

Assume that the following three tables have been created:
CREATE TABLE t1 (c1 int, c2 varchar(10), c3 numeric(4,2));
CREATE TABLE t2 (c1 bigint, c2 char(8), c4 numeric(6,3));
CREATE TABLE t3 (c3 bigint, c5 int, c6 numeric(6,3));
  • For a join with an ON clause:
    SELECT * FROM t1 INNER JOIN t2 ON t1.c1 = t2.c1 AND t1.c2 = t2.c2;
    Column projections: t1.c1, t1.c2, t1.c3, t2.c1, t2.c2, t2.c4
    SELECT * FROM t1 FULL JOIN t2 ON t1.c1 = t2.c1 AND t1.c2 = t2.c2;
    Column projections: t1.c1, t1.c2, t1.c3, t2.c1, t2.c2, t2.c4
  • For an inner join or left outer join with a USING clause:
    SELECT * FROM t1 INNER JOIN t2 USING (c1, c2);
    Column projections: t1.c1, t1.c2, t1.c3, t2.c4
    SELECT * FROM t1 LEFT JOIN t2 USING (c1, c2);
    Column projections: t1.c1, t1.c2, t1.c3, t2.c4
  • For a right outer join with a USING clause:
    SELECT * FROM t1 RIGHT JOIN t2 USING (c1, c2);
    Column projections: t2.c1, t2.c2, t1.c3, t2.c4
  • For a full outer join with a USING clause:
    SELECT * FROM t1 FULL JOIN t2 USING (c1, c2);
    Column projections: 
      CASE WHEN (t1.c1 IS NOT NULL) THEN t1.c1 ELSE t2.c1 END AS c1
      CASE WHEN (t1.c2 IS NOT NULL) THEN t1.c2 ELSE t2.c2 END AS c2
      t1.c3
      t2.c4