LATERAL

constructsupportedQueries & DML

docs.snowflake.com ↗

In a FROM clause, the LATERAL keyword allows an inline view to reference columns from a table expression that precedes that inline view. A lateral join behaves more like a correlated subquery than a typical join. A lateral join behaves as if the server executed a loop similar to the following: Unlike the output of a non-lateral join, the output from a lateral join includes only the rows generated from the inline view. There is no need for an explicit ON clause to join rows from the left-hand side to the right-hand side; the relationship is already established because the inline view references columns from the left-hand table expression. See also: Using lateral joins.

Syntax

SELECT ...
  FROM <left_hand_table_expression>, LATERAL ( <inline_view> )
...

Parameters

NameRequiredDescription
left_hand_table_expressionThis is a source of rows, such as: A table. A view. A subquery. A table function. The result of an earlier join.
inline_viewThe inline_view can be: An inline view: a view defined within the statement, and valid only for the duration of the statement. A subquery. A table function: either a built-in table function such as FLATTEN or a user-defined table function (UDTF). The inline_view can’t be a plain table reference. It must be an expression that can process or filter rows based on values from the left-hand table expression, such as a subquery with a WHERE clause or a table function call.

Usage notes

When to use LATERAL

LATERAL is a valuable tool for the following use cases: - Chaining table functions on nested data: When you need to flatten arrays within arrays or navigate multiple levels of nested JSON, each subsequent table function call must reference the output of the previous one. Lateral joins make this possible. - Calling table functions with row-specific arguments: When a table function (such as a UDTF) needs to receive different input values for each row from the left-hand table. For simple cases such as flattening a single-level array, using TABLE(FLATTEN(...)) without a lateral join produces the same result. Lateral joins are necessary only when the inline view must reference columns that are only available from a preceding expression in the FROM clause.

Parameters

left_hand_table_expression: This is a source of rows, such as: A table. A view. A subquery. A table function. The result of an earlier join. inline_view: The inline_view can be: An inline view: a view defined within the statement, and valid only for the duration of the statement. A subquery. A table function: either a built-in table function such as FLATTEN or a user-defined table function (UDTF). The inline_view can’t be a plain table reference. It must be an expression that can process or filter rows based on values from the left-hand table expression, such as a subquery with a WHERE clause or a table function call.

LATERAL versus other approaches

The following table summarizes when to use LATERAL compared to other approaches: Use case | Recommendation Flatten a single-level array | TABLE(FLATTEN(...)) without LATERAL works the same. LATERAL is optional. Flatten nested arrays (arrays within arrays) | LATERAL is required to chain FLATTEN calls. Filter rows from another table based on the current row | Either a correlated subquery in the SELECT list or LATERAL works. LATERAL can return multiple rows and columns; a correlated subquery in SELECT can’t do this. Call a table function with row-specific input | LATERAL allows the table function to receive different arguments for each row.