JOIN
constructsupportedQueries & DMLA JOIN operation combines rows from two tables — or other table-like sources, such as views or table functions — to create a new combined row that can be used in the query. For a conceptual explanation of joins, see Working with joins. This topic describes how to use the JOIN subclause in the FROM clause. The JOIN subclause specifies, explicitly or implicitly, how to relate rows in one table to the corresponding rows in the other table. You can also use the ASOF JOIN subclause, which is used to join time-series data on timestamp columns when their values closely follow each other, precede each other, or match exactly. Although the recommended way to join tables is to use JOIN with the ON subclause of the FROM clause, an alternative way to join tables is to use the WHERE clause. For details, see the documentation for the WHERE clause.
Syntax
supportedpartialnot supportednot scored
Use one of the following
SELECT ...
FROM <object_ref1> [
{
INNER
| { LEFT | RIGHT | FULL } [ OUTER ]
}
[ DIRECTED ]
]
JOIN <object_ref2>
[ ON <condition> ]
[ { FIRST | LAST } ( <alias> ) ]
[ ... ]
Use one of the following
SELECT *
FROM <object_ref1> [
{
INNER
| { LEFT | RIGHT | FULL } [ OUTER ]
}
[ DIRECTED ]
]
JOIN <object_ref2>
[ USING( <column_list> ) ]
[ { FIRST | LAST } ( <alias> ) ]
[ ... ]
Use one of the following
SELECT ...
FROM <object_ref1> [
{
NATURAL [
{
INNER
| { LEFT | RIGHT | FULL } [ OUTER ]
}
[ DIRECTED ]
]
| CROSS [ DIRECTED ]
}
]
JOIN <object_ref2>
[ ... ]
Parameters
| Name | Required | Description |
|---|---|---|
object_ref1 and object_ref2 | Each object reference is a table or table-like data source. | |
JOIN | Use the JOIN keyword to specify that the tables should be joined. Combine JOIN with other join-related keywords — for example, INNER or OUTER — to specify the type of join. The semantics of joins are as follows (for brevity, this topic uses o1 and o2 for object_ref1 and object_ref2, respectively). Join Type Semantics o1 INNER JOIN o2 For each row of o1, a row is produced for each row of o2 that matches according to the ON condition subclause. (You can also use a comma to specify an inner join. For an example, see the examples section.) If you use INNER JOIN without the ON clause, or if you use a comma without a WHERE clause, the result is the same as using CROSS JOIN: a Cartesian product; every row of o1 paired with every row of o2. o1 LEFT OUTER JOIN o2 The result of the inner join is augmented with a row for each row of o1 that has no matches in o2. The result columns referencing o2 contain null. o1 RIGHT OUTER JOIN o2 The result of the inner join is augmented with a row for each row of o2 that has no matches in o1. The result columns referencing o1 contain null. o1 FULL OUTER JOIN o2 Returns all joined rows, plus one row for each unmatched left side row (extended with nulls on the right), plus one row for each unmatched right side row (extended with nulls on the left). o1 CROSS JOIN o2 For every possible combination of rows from o1 and o2 (that is, Cartesian product), the joined table contains a row consisting of all columns in o1 followed by all columns in o2. A CROSS JOIN can’t be combined with an ON condition clause. However, you can use a WHERE clause to filter the results. o1 NATURAL JOIN o2 A NATURAL JOIN is identical to an explicit JOIN on the common columns of the two tables, except that the common columns are included only once in the output. (A natural join assumes that columns with the same name, but in different tables, contain corresponding data.) For examples, see the examples section. A NATURAL JOIN can be combined with an OUTER JOIN. A NATURAL JOIN can’t be combined with an ON condition clause because the JOIN condition is already implied. However, you can use a WHERE clause to filter the results.ExpandShow lessSee more The DIRECTED keyword specifies a directed join, which enforces the join order of the tables. The first, or left, table is scanned before the second, or right, table. For example, o1 INNER DIRECTED JOIN o2 scans the o1 table before the o2 table. Directed joins are useful in the following situations: You are migrating workloads into Snowflake that have join order directives. You want to improve performance by scanning join tables in a specific order. To choose which table is scanned first without rewriting the order in which you write the tables, add a read-order clause after the ON or USING clause. For details, see the read-order clause. Default: INNER JOIN If the word JOIN is used without specifying INNER or OUTER, then the JOIN is an inner join. If the DIRECTED keyword is added, the join type (for example, INNER, LEFT, RIGHT, or FULL) is required. See also: LATERAL ASOF JOIN | |
ON condition | A Boolean expression that defines the rows from the two sides of the JOIN that are considered to match, for example:Copy code ON object_ref2.id_number = object_ref1.id_number Conditions are discussed in more detail in the WHERE clause documentation. The ON clause is prohibited for CROSS JOIN. The ON clause is unnecessary, and prohibited, for NATURAL JOIN because the join columns are implied. For other joins, the ON clause is optional. However, omitting the ON clause results in a Cartesian product; every row of object_ref1 paired with every row of object_ref2. A Cartesian product can produce a very large volume of output, almost all of which consists of pairs of rows that aren’t actually related, which consumes a lot of resources and is often a user error. | |
USING( column_list ) | A list of columns in common between the two tables being joined. These columns are used as the join columns. The columns must have the same name and meaning in each of the tables being joined. For example, suppose that the SQL statement contains:Copy code ... o1 JOIN o2 USING (key_column) In the simple case, this would be equivalent to:Copy code ... o1 JOIN o2 ON o2.key_column = o1.key_column In the standard JOIN syntax, the projection list (the list of columns and other expressions after the SELECT keyword) is *. This causes the query to return the key_column exactly once. The columns are returned in the following order: The columns in the USING clause in the order specified. The left table columns not specified in the USING clause. The right table columns not specified in the USING clause. For examples of standard and nonstandard usage, see the examples section. | |
{ FIRST | LAST } ( alias ) | Specifies which side of a directed join is scanned first. This clause is called the read-order clause. FIRST( alias ) names the table that is scanned first, and LAST( alias ) names the table that is scanned last. A join has two sides, so the two forms are interchangeable. For o1 INNER DIRECTED JOIN o2, specifying FIRST(o2) is equivalent to specifying LAST(o1). Use whichever form is clearer. You can specify only one read-order clause for a single join. The alias must name one of the two tables that the join combines. If the table doesn’t have an explicit alias, use the table name. This clause is useful when you want to set the scan order of one join inside a chain of joins. Without it, you must restructure the query: move a table to the front of the FROM clause, parenthesize the remaining join, and move the join condition to change which side is scanned first. For an example, see Set the scan order for a directed join. For restrictions, see the usage notes. Default: The left table is scanned first. |
Usage notes
- The following restrictions apply to table functions other than SQL UDTFs: You can’t specify the ON, USING, or NATURAL JOIN clause in a lateral table function, other than a SQL UDTF. For example, the following syntax is not allowed: Copy code SELECT ... FROM my_table JOIN TABLE(FLATTEN(input=>[col_a])) ON ... ; Copy code SELECT ... FROM my_table INNER JOIN TABLE(FLATTEN(input=>[col_a])) ON ... ; Copy code SELECT ... FROM my_table JOIN TABLE(my_js_udtf(col_a)) ON ... ; Copy code SELECT ... FROM my_table INNER JOIN TABLE(my_js_udtf(col_a)) ON ... ;
- You can’t specify the ON, USING, or NATURAL JOIN clause in an outer lateral join to a table function, other than a SQL UDTF. For example, the following syntax is not allowed: Copy code SELECT ... FROM my_table LEFT JOIN TABLE(FLATTEN(input=>[a])) ON ... ; Copy code SELECT ... FROM my_table FULL JOIN TABLE(FLATTEN(input=>[a])) ON ... ; Copy code SELECT ... FROM my_table LEFT JOIN TABLE(my_js_udtf(a)) ON ... ; Copy code SELECT ... FROM my_table FULL JOIN TABLE(my_js_udtf(a)) ON ... ; Using this syntax results in the following error: 000002 (0A000): Unsupported feature 'lateral table function called with OUTER JOIN syntax or a join predicate (ON clause)'
- These restrictions don’t apply if you are using a comma, rather than a JOIN keyword: Copy code SELECT ... FROM my_table, TABLE(FLATTEN(input=>[col_a])) ON ... ;
- The following restrictions apply to the read-order clause ({ FIRST | LAST } ( alias )): The join must specify the DIRECTED keyword and an ON or USING clause. Using FIRST or LAST on a join that isn’t directed returns the following error: 001867 (42601): SQL compilation error: FIRST or LAST is only valid on a DIRECTED JOIN.
- The clause isn’t supported for CROSS DIRECTED JOIN, NATURAL ... DIRECTED JOIN, or a directed join that omits the ON and USING clauses. In those positions, FIRST or LAST is parsed as an alias for the second table rather than as a read-order clause. If that table has no alias of its own, the statement succeeds and the requested scan order is silently ignored; if it already has one, the statement fails with a syntax error. Add an ON or USING clause to set the scan order.
- The alias must name one of the two tables that the join combines. When one side of the join is itself a join (for example, the accumulated result of earlier joins in a chain), that side can’t be named, so name the table on the other side instead. Naming anything else returns the following error: 001868 (42601): SQL compilation error: FIRST or LAST requires two direct operands, and alias 'my_alias' must name one of them.
- You can specify at most one read-order clause for a single join, but you can specify a read-order clause on more than one join in the same query.
- FIRST and LAST aren’t reserved keywords. You can continue to use them as table names, column names, and aliases.
Parameters
object_ref1 and object_ref2: Each object reference is a table or table-like data source. JOIN: Use the JOIN keyword to specify that the tables should be joined. Combine JOIN with other join-related keywords — for example, INNER or OUTER — to specify the type of join. The semantics of joins are as follows (for brevity, this topic uses o1 and o2 for object_ref1 and object_ref2, respectively). Join Type Semantics o1 INNER JOIN o2 For each row of o1, a row is produced for each row of o2 that matches according to the ON condition subclause. (You can also use a comma to specify an inner join. For an example, see the examples section.) If you use INNER JOIN without the ON clause, or if you use a comma without a WHERE clause, the result is the same as using CROSS JOIN: a Cartesian product; every row of o1 paired with every row of o2. o1 LEFT OUTER JOIN o2 The result of the inner join is augmented with a row for each row of o1 that has no matches in o2. The result columns referencing o2 contain null. o1 RIGHT OUTER JOIN o2 The result of the inner join is augmented with a row for each row of o2 that has no matches in o1. The result columns referencing o1 contain null. o1 FULL OUTER JOIN o2 Returns all joined rows, plus one row for each unmatched left side row (extended with nulls on the right), plus one row for each unmatched right side row (extended with nulls on the left). o1 CROSS JOIN o2 For every possible combination of rows from o1 and o2 (that is, Cartesian product), the joined table contains a row consisting of all columns in o1 followed by all columns in o2. A CROSS JOIN can’t be combined with an ON condition clause. However, you can use a WHERE clause to filter the results. o1 NATURAL JOIN o2 A NATURAL JOIN is identical to an explicit JOIN on the common columns of the two tables, except that the common columns are included only once in the output. (A natural join assumes that columns with the same name, but in different tables, contain corresponding data.) For examples, see the examples section. A NATURAL JOIN can be combined with an OUTER JOIN. A NATURAL JOIN can’t be combined with an ON condition clause because the JOIN condition is already implied. However, you can use a WHERE clause to filter the results.ExpandShow lessSee more The DIRECTED keyword specifies a directed join, which enforces the join order of the tables. The first, or left, table is scanned before the second, or right, table. For example, o1 INNER DIRECTED JOIN o2 scans the o1 table before the o2 table. Directed joins are useful in the following situations: You are migrating workloads into Snowflake that have join order directives. You want to improve performance by scanning join tables in a specific order. To choose which table is scanned first without rewriting the order in which you write the tables, add a read-order clause after the ON or USING clause. For details, see the read-order clause. Default: INNER JOIN If the word JOIN is used without specifying INNER or OUTER, then the JOIN is an inner join. If the DIRECTED keyword is added, the join type (for example, INNER, LEFT, RIGHT, or FULL) is required. See also: LATERAL ASOF JOIN ON condition: A Boolean expression that defines the rows from the two sides of the JOIN that are considered to match, for example:Copy code ON object_ref2.id_number = object_ref1.id_number Conditions are discussed in more detail in the WHERE clause documentation. The ON clause is prohibited for CROSS JOIN. The ON clause is unnecessary, and prohibited, for NATURAL JOIN because the join columns are implied. For other joins, the ON clause is optional. However, omitting the ON clause results in a Cartesian product; every row of object_ref1 paired with every row of object_ref2. A Cartesian product can produce a very large volume of output, almost all of which consists of pairs of rows that aren’t actually related, which consumes a lot of resources and is often a user error. USING( column_list ): A list of columns in common between the two tables being joined. These columns are used as the join columns. The columns must have the same name and meaning in each of the tables being joined. For example, suppose that the SQL statement contains:Copy code ... o1 JOIN o2 USING (key_column) In the simple case, this would be equivalent to:Copy code ... o1 JOIN o2 ON o2.key_column = o1.key_column In the standard JOIN syntax, the projection list (the list of columns and other expressions after the SELECT keyword) is *. This causes the query to return the key_column exactly once. The columns are returned in the following order: The columns in the USING clause in the order specified. The left table columns not specified in the USING clause. The right table columns not specified in the USING clause. For examples of standard and nonstandard usage, see the examples section. { FIRST | LAST } ( alias ): Specifies which side of a directed join is scanned first. This clause is called the read-order clause. FIRST( alias ) names the table that is scanned first, and LAST( alias ) names the table that is scanned last. A join has two sides, so the two forms are interchangeable. For o1 INNER DIRECTED JOIN o2, specifying FIRST(o2) is equivalent to specifying LAST(o1). Use whichever form is clearer. You can specify only one read-order clause for a single join. The alias must name one of the two tables that the join combines. If the table doesn’t have an explicit alias, use the table name. This clause is useful when you want to set the scan order of one join inside a chain of joins. Without it, you must restructure the query: move a table to the front of the FROM clause, parenthesize the remaining join, and move the join condition to change which side is scanned first. For an example, see Set the scan order for a directed join. For restrictions, see the usage notes. Default: The left table is scanned first.