Inner Join
An inner join is an operation that produces new rows by combining rows from two or more tables, based on the join predicates applied to related columns or fields between them. The result-set contains only those combined rows that satisfy the join predicates.
Syntax
innerjoin_from_clause ::= FROM innerjoin_tables
(COMMA innerjoin_tables
)* (COMMA unnest_syntax
)*
innerjoin_table_clause ::= single_from_table
|aliased_table_name
|left_outer_join_tables
|nested_tables
For description of various elements of the syntax, see FROM clause. Note that the syntax implies that we can not only join individual tables, but also the result of a NESTED TABLES clause (or left outer join list) with an individual table, or with the result of another NESTED TABLES clause (or left outer join list).
Semantics
The FROM clause specifies the participating tables. The name of any participating table in the join may be followed by a table alias.
Let us consider that we perform an inner join of two tables A and B. Table A has N rows and n columns, and table B has M rows and m columns. Conceptually, every row in table A is joined with every row in table B. The resultant table AB would have N * M rows and n + m columns.
Table 6-10 Table A
CA1 | CA2 | CA3 |
---|---|---|
RA11 | RA12 | RA13 |
RA21 | RA22 | RA23 |
RA31 | RA32 | RA33 |
Table 6-11 Table B
(CA1) | CB1 | CB2 | CB3 |
---|---|---|---|
RA11 | RB11 | RB12 | RB13 |
RA21 | RB21 | RB22 | RB23 |
Table 6-12 Combined Table AB (every row in table A is combined with every row in table B)
CA1 | CA2 | CA3 | (CA1) | CB1 | CB2 | CB3 |
---|---|---|---|---|---|---|
RA11 | RA12 | RA13 | RA11 | RB11 | RB12 | RB13 |
RA11 | RA12 | RA13 | RA21 | RB21 | RB22 | RB23 |
RA21 | RA22 | RA23 | RA11 | RB11 | RB12 | RB13 |
RA21 | RA22 | RA23 | RA21 | RB21 | RB22 | RB23 |
RA31 | RA32 | RA33 | RA11 | RB11 | RB12 | RB13 |
RA31 | RA32 | RA33 | RA21 | RB21 | RB22 | RB23 |
Table 6-13 Result after inner join (based on shard key equality predicate)
CA1 | CA2 | CA3 | (CA1) | CB1 | CB2 | CB3 |
---|---|---|---|---|---|---|
RA11 | RA12 | RA13 | RA11 | RB11 | RB12 | RB13 |
RA21 | RA22 | RA23 | RA21 | RB21 | RB22 | RB23 |
When joining more than 2 tables, say A, B, C, and D, the FROM clause is applied first to tables A and B producing table AB. In a similar manner, the FROM clause is applied to tables AB and C, producing table ABC. Lastly, the FROM clause is applied to ABC and D, producing table ABCD. The join predicates in the WHERE clause are then applied to this combined table ABCD.
- Only joins among tables in the same table hierarchy are allowed.
- Supports joining of tables that are in an ancestor-descendant relationship as well as tables that are not in an ancestor-descendant relationship.
- The join predicates must include equality predicates between all the shard key columns of the joined tables. That is, for any pair of joined tables, a row from one table will match with a row from the other table only if they both have the same values on their shard key columns. To understand more about shard keys, see CREATE TABLE . You can use the DESCRIBE TABLE statement to identify the shard keys.
- The rest of the predicates in the WHERE clause are applied to these matching rows.
Ordering of columns in the result-set of inner join:
If you use a '*' with the SELECT clause, wherein all the fields in the tables are returned, the order of fields in the result-set depends on the order in which you specify the tables in the FROM clause. If you provide a list of fields in the SELECT clause, then the order of the fields is as specified in the SELECT clause.