dataengref migrate

Home / SQL Server

SQL Server CROSS APPLY in Snowflake, Databricks and BigQuery

CROSS APPLY becomes a lateral join: LATERAL in Snowflake and Databricks. BigQuery only allows correlated joins against arrays, so the subquery is wrapped in UNNEST(ARRAY(SELECT AS STRUCT ...)).

SQL Server syntax: FROM left_table CROSS APPLY (subquery or table_valued_function) · SQL Server docs

Equivalents

TargetSupportSyntaxNotesSource
SnowflakeRewriteFROM t, LATERAL (subquery)The inline view can reference columns of tables to its left.Snowflake docs
DatabricksRewriteFROM t JOIN LATERAL (subquery)LATERAL subqueries can reference columns of preceding tables.Databricks docs
BigQueryRewriteFROM t JOIN UNNEST(ARRAY(SELECT AS STRUCT ... WHERE ... = t.col))Correlated joins must reference an array on the right side.BigQuery docs

Behavior differences to check

Examples

Latest order per customer

SQL Server
SELECT c.id, o.order_id FROM customers c CROSS APPLY (SELECT TOP 1 order_id FROM orders WHERE orders.customer_id = c.id ORDER BY ordered_at DESC) o;
Snowflake
SELECT c.id, o.order_id FROM customers c, LATERAL (SELECT order_id FROM orders WHERE orders.customer_id = c.id ORDER BY ordered_at DESC LIMIT 1) o;
Databricks
SELECT c.id, o.order_id FROM customers c JOIN LATERAL (SELECT order_id FROM orders WHERE orders.customer_id = c.id ORDER BY ordered_at DESC LIMIT 1) o;
BigQuery
SELECT customer_id AS id, order_id FROM orders WHERE TRUE QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY ordered_at DESC) = 1;

Other SQL Server functions

Last checked: · Paste a whole query into the SQL checker to find every function that needs a change.