<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Lateral_column_alias</id>
	<title>Lateral column alias - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Lateral_column_alias"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Lateral_column_alias&amp;action=history"/>
	<updated>2026-12-06T00:07:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Lateral_column_alias&amp;diff=156&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Lateral_column_alias&amp;diff=156&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:44Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Lateral column alias&lt;br /&gt;
| also_known_as   = LCA, reusable column alias, lateral alias reference&lt;br /&gt;
| field           = SQL name resolution&lt;br /&gt;
| key_parts       = Alias defined earlier in the same SELECT list; resolution after table columns&lt;br /&gt;
| implementations = [[Apache Spark]] (since 3.4.0), Databricks SQL, [[DuckDB]], Snowflake&lt;br /&gt;
| related         = [[Window functions]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-name-resolution.md, SQLConf.scala, error-conditions.json), the Spark 3.4.0 and 3.5.0 release notes, the Databricks name resolution reference, DuckDB 2.0 friendly SQL page, Snowflake SELECT reference, BigQuery query syntax reference and PostgreSQL 19 SELECT reference}}&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;lateral column alias&amp;#039;&amp;#039;&amp;#039; is a column alias defined in a &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; list that later expressions in the same &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; list can refer to, as in &amp;lt;code&amp;gt;SELECT price * qty AS total, total * 0.2 AS tax&amp;lt;/code&amp;gt;; [[Apache Spark]] supports it since version 3.4.0.&amp;lt;ref name=&amp;quot;r340&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-4-0.html |title=Spark Release 3.4.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;nameres&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-name-resolution.html |title=Name resolution (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; It saves repeating an expression or wrapping the query in a subquery. Several engines, among them BigQuery and PostgreSQL, do not allow it,&amp;lt;ref name=&amp;quot;bq&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;pg&amp;quot; /&amp;gt; so the same query can work in one engine and fail in another.&lt;br /&gt;
&lt;br /&gt;
== How does Spark resolve a lateral column alias? ==&lt;br /&gt;
Spark SQL treats lateral column aliasing as one step of its name resolution order. Inside a &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; list, Spark first tries to match an identifier to a column of the tables in the &amp;lt;code&amp;gt;FROM&amp;lt;/code&amp;gt; clause; only if that fails does it match the leading identifier to a preceding alias in the same &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; list.&amp;lt;ref name=&amp;quot;nameres&amp;quot; /&amp;gt; A real column with the same name therefore wins over an alias. Spark&amp;#039;s documentation shows that &amp;lt;code&amp;gt;SELECT c1 AS a, a + c1 FROM VALUES(2, 3) AS T(c1, a)&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;2, 5&amp;lt;/code&amp;gt;, because &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; resolves to the table column (3), not to the alias.&amp;lt;ref name=&amp;quot;nameres&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an identifier matches more than one preceding alias, Spark raises &amp;lt;code&amp;gt;AMBIGUOUS_LATERAL_COLUMN_ALIAS&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;nameres&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;errors&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/common/utils/src/main/resources/error/error-conditions.json |title=error-conditions.json (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Further parts of a dotted name after the alias are resolved as struct fields or map keys.&amp;lt;ref name=&amp;quot;nameres&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The behaviour is controlled by &amp;lt;code&amp;gt;spark.sql.lateralColumnAlias.enableImplicitResolution&amp;lt;/code&amp;gt;, an internal configuration added in 3.4.0 whose default is &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History in Spark ==&lt;br /&gt;
Spark 3.4.0 added lateral column alias references (SPARK-27561).&amp;lt;ref name=&amp;quot;r340&amp;quot; /&amp;gt; Spark 3.5.0 extended them to aggregate queries (SPARK-41631) and to queries with window functions (SPARK-42217).&amp;lt;ref name=&amp;quot;r350&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-5-0.html |title=Spark Release 3.5.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Databricks documents the same rule for Databricks SQL and Databricks Runtime 12.2 LTS and above.&amp;lt;ref name=&amp;quot;dbx&amp;quot;&amp;gt;{{Cite web |url=https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-name-resolution |title=Name resolution |publisher=Databricks |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What are the limits in Spark? ==&lt;br /&gt;
Spark 4.3 rejects some placements of a lateral alias with an &amp;lt;code&amp;gt;UNSUPPORTED_FEATURE&amp;lt;/code&amp;gt; error. The error conditions name these cases:&amp;lt;ref name=&amp;quot;errors&amp;quot; /&amp;gt;&lt;br /&gt;
* inside the arguments of an aggregate function (&amp;lt;code&amp;gt;LATERAL_COLUMN_ALIAS_IN_AGGREGATE_FUNC&amp;lt;/code&amp;gt;);&lt;br /&gt;
* inside a window expression (&amp;lt;code&amp;gt;LATERAL_COLUMN_ALIAS_IN_WINDOW&amp;lt;/code&amp;gt;);&lt;br /&gt;
* inside a generator expression such as &amp;lt;code&amp;gt;explode&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;LATERAL_COLUMN_ALIAS_IN_GENERATOR&amp;lt;/code&amp;gt;);&lt;br /&gt;
* through &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt; alias or &amp;lt;code&amp;gt;GROUP BY ALL&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;LATERAL_COLUMN_ALIAS_IN_GROUP_BY&amp;lt;/code&amp;gt;);&lt;br /&gt;
* in an aggregate query that has both window expressions and a &amp;lt;code&amp;gt;HAVING&amp;lt;/code&amp;gt; clause (&amp;lt;code&amp;gt;LATERAL_COLUMN_ALIAS_IN_AGGREGATE_WITH_WINDOW_AND_HAVING&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
A lateral alias may refer to the result of an aggregate, as in the second example below; it may not be fed into one.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Spark SQL, using an alias in a later expression of the same &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT name,&lt;br /&gt;
       salary * 12        AS annual,&lt;br /&gt;
       annual * 0.1       AS bonus&lt;br /&gt;
FROM employees;&lt;br /&gt;
&lt;br /&gt;
-- An alias of an aggregate result, reused in the same SELECT list&lt;br /&gt;
SELECT dept,&lt;br /&gt;
       SUM(salary)        AS total,&lt;br /&gt;
       total / COUNT(*)   AS avg_salary&lt;br /&gt;
FROM employees&lt;br /&gt;
GROUP BY dept;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In engines without lateral aliases, the first query has to repeat &amp;lt;code&amp;gt;salary * 12&amp;lt;/code&amp;gt; or compute &amp;lt;code&amp;gt;annual&amp;lt;/code&amp;gt; in a subquery.&lt;br /&gt;
&lt;br /&gt;
== Which engines allow lateral column aliases? ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Engine !! Alias reusable in the same SELECT list? !! Notes&lt;br /&gt;
|-&lt;br /&gt;
| Apache Spark (3.4.0 and later)&lt;br /&gt;
| Yes&amp;lt;ref name=&amp;quot;r340&amp;quot; /&amp;gt;&lt;br /&gt;
| Table columns take precedence over aliases; not allowed inside aggregate, window or generator arguments&amp;lt;ref name=&amp;quot;nameres&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;errors&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Databricks SQL and Databricks Runtime&lt;br /&gt;
| Yes, Databricks Runtime 12.2 LTS and above&amp;lt;ref name=&amp;quot;dbx&amp;quot; /&amp;gt;&lt;br /&gt;
| Same rule as Spark: local column references take precedence&amp;lt;ref name=&amp;quot;dbx&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| DuckDB 2.0&lt;br /&gt;
| Yes&amp;lt;ref name=&amp;quot;duck&amp;quot;&amp;gt;{{Cite web |url=https://duckdb.org/docs/2.0/sql/dialect/friendly_sql |title=Friendly SQL (DuckDB 2.0) |publisher=DuckDB Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Also allows aliases in &amp;lt;code&amp;gt;WHERE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;HAVING&amp;lt;/code&amp;gt;, but not in a join&amp;#039;s &amp;lt;code&amp;gt;ON&amp;lt;/code&amp;gt; clause&amp;lt;ref name=&amp;quot;duck&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Snowflake&lt;br /&gt;
| Yes&amp;lt;ref name=&amp;quot;snow&amp;quot;&amp;gt;{{Cite web |url=https://docs.snowflake.com/en/sql-reference/sql/select |title=SELECT |publisher=Snowflake |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Aliases can be used in other column expressions and in &amp;lt;code&amp;gt;WHERE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt; and joins; the documentation advises against aliases that share a name with a referenced column&amp;lt;ref name=&amp;quot;snow&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| BigQuery (GoogleSQL)&lt;br /&gt;
| No&amp;lt;ref name=&amp;quot;bq&amp;quot;&amp;gt;{{Cite web |url=https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax |title=Query syntax (GoogleSQL for BigQuery) |publisher=Google Cloud |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; list aliases are visible only to &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ORDER BY&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;HAVING&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;bq&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| PostgreSQL 19&lt;br /&gt;
| No; output names are documented only for &amp;lt;code&amp;gt;ORDER BY&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;pg&amp;quot;&amp;gt;{{Cite web |url=https://www.postgresql.org/docs/current/sql-select.html |title=SELECT (PostgreSQL 19) |publisher=PostgreSQL Global Development Group |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| The documentation says an output column name cannot be used in &amp;lt;code&amp;gt;WHERE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;HAVING&amp;lt;/code&amp;gt;; the expression must be written out&amp;lt;ref name=&amp;quot;pg&amp;quot; /&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Because precedence rules differ, a query that relies on a lateral alias whose name also matches a table column can return different results across engines. Choosing alias names that do not collide with column names avoids this.&amp;lt;ref name=&amp;quot;nameres&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;snow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Window functions]]&lt;br /&gt;
* [[Apache Spark]]&lt;br /&gt;
* [[DuckDB]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;br /&gt;
[[Category:Query engines]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>