Snowflake 5
Snowflake 5
Categories:
Query syntax
LATERAL
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 like most joins. 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. The rows on the left-hand side do not need to be
joined to the right hand side because the rows on the left-hand side have already been
taken into account by being passed into the inline view.
Syntax
SELECT ...
FROM <left_hand_table_expression>, LATERAL ( <inline_view> )
...
Parameters
<left_hand_table_expression>
<inline_view>
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.
SELECT *
FROM table_reference_me, LATERAL (...), table_do_not_reference_m
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.
You cannot specify the ON, USING, or NATURAL JOIN clause in a lateral table
function (other than a SQL UDTF), and you cannot specify the ON, USING, or
NATURAL JOIN clause in an outer lateral join to a table function (other than a SQL
UDTF).
For details, refer to the Usage Notes in the JOIN topic.
Examples
This example uses the data shown below:
Basic example
This example shows a lateral join with a subquery.
SELECT *
FROM departments AS d, LATERAL (SELECT * FROM employees AS e WHERE
ORDER BY employee_ID;
+---------------+-------------+-------------+-----------+-------------
| DEPARTMENT_ID | NAME | EMPLOYEE_ID | LAST_NAME | DEPARTMENT_I
|---------------+-------------+-------------+-----------+-------------
| 1 | Engineering | 101 | Richards |
| 1 | Engineering | 102 | Paulson |
| 2 | Support | 103 | Johnson |
+---------------+-------------+-------------+-----------+-------------
First, update the employee table to include ARRAY data that FLATTEN can operate on:
Second, execute a query that uses FLATTEN and contains a reference to a column(s)
in a table that precedes it:
SELECT emp.employee_ID, emp.last_name, index, value AS project_name
FROM employees AS emp, LATERAL FLATTEN(INPUT => emp.project_names)
ORDER BY employee_ID;
+-------------+-----------+-------+----------------------+
| EMPLOYEE_ID | LAST_NAME | INDEX | PROJECT_NAME |
|-------------+-----------+-------+----------------------|
| 101 | Richards | 0 | "Materialized Views" |
| 101 | Richards | 1 | "UDFs" |
| 102 | Paulson | 0 | "Materialized Views" |
| 102 | Paulson | 1 | "Lateral Joins" |
+-------------+-----------+-------+----------------------+
SELECT *
FROM departments AS d, LATERAL (SELECT * FROM employees AS e WHERE
ORDER BY employee_ID;
SELECT *
FROM departments AS d INNER JOIN LATERAL (SELECT * FROM employees
ORDER BY employee_ID;
+---------------+-------------+-------------+-----------+-------------
| DEPARTMENT_ID | NAME | EMPLOYEE_ID | LAST_NAME | DEPARTMENT_I
|---------------+-------------+-------------+-----------+-------------
| 1 | Engineering | 101 | Richards |
| 1 | Engineering | 102 | Paulson |
| 2 | Support | 103 | Johnson |
+---------------+-------------+-------------+-----------+-------------