natural_join
natural_join joins data from a pair of tables - a left and right table - based upon one or more match columns. The match columns establish key identifiers in the left table that will be used to find data in the right table. Any data type which implements Java equality and hashCode can be used as a key.
The output table contains all of the rows and columns of the left table plus additional columns containing data from the right table. For columns appended to the left table, row values equal the row values from the right table where the key values in the left and right tables are equal. If there is no matching key in the right table, appended row values are NULL. If there are multiple matches, the operation will fail by default but can be configured to match either the first or last matching row.
Syntax
Parameters
| Parameter | Type | Description |
|---|---|---|
| table | Table | The table data is added from (the right table). |
| on | Union[str, Sequence[str]] | Columns from the left and right tables used to join on.
|
| joins optional | Union[str, Sequence[str]] | Columns from the right table to be added to the left table based on key may be specified in this list:
|
| type optional | NaturalJoinType | How to handle duplicate keys in the right table. If omitted, the default is to error on duplicates ( The following options are available:
|
Returns
A new table containing all of the rows and columns of the left table, plus additional columns containing data from the right table. For columns appended to the left table, row values equal the row values from the right table where the key values in the left and right tables are equal. If there is no matching key in the right table, appended row values are NULL.
Examples
In the following example, the left and right tables are joined on a matching column named DeptID.
If the right table has columns that need renaming due to an initial name match, a new column name can be supplied in the third argument of the join. In the following example, Telephone in the right table is renamed to DeptTelephone.
In some cases, the matching columns have different names in the left and right table. Below, the left table has a column named DeptNumber that needs to be matched to the column DeptID in the right table. To perform this match, the second argument needs the name of each column in the left and right tables.
Handling duplicate right table rows can be done by specifying the type parameter. In the following example, the FIRST_MATCH option is used to match the first right table rows and ignore later duplicates.
In the following example, the LAST_MATCH option is used to match the last right table rows and ignore earlier duplicates.