LATERAL
constructsupportedQueries & DMLIn 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
| Name | Required | Description |
|---|---|---|
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. |
Usage notes
- The inline view after the keyword LATERAL can reference columns only from the inline view itself and from tables to the left of the inline view in the FROM clause. Copy code SELECT * FROM table_reference_me, LATERAL (...), table_do_not_reference_me ...
- Although the inline view typically references one or more columns from the left_hand_table_expression, it is not required to do so.
- Just as INNER JOIN syntax can use either the comma or the keywords INNER JOIN, a lateral join can also use the comma or the keywords INNER JOIN. For example: Copy code FROM departments AS d INNER JOIN LATERAL (...)
- You can’t specify the ON, USING, or NATURAL JOIN clause in: A lateral table function (other than a SQL UDTF)
- An outer lateral join to a table function (other than a SQL UDTF)
- For details, see the usage notes in the JOIN topic.
- The left_hand_table_expression can’t be an UNPIVOT result set. Attempting to reference an UNPIVOT alias in a LATERAL join causes an error. As a workaround, materialize the UNPIVOT result into a temporary table first, then use that table as the left-hand expression. For more information, see UNPIVOT.
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.