Updated on 2026-11-06 GMT+08:00

FROM

Function

The FROM clause is used to specify the data source for a query. Spark 4.0 enhances the FROM clause, supporting LATERAL subqueries, the TABLE keyword, VALUES row constructors, and other data source forms.

Syntax

FROM table_reference [, ...]

Where table_reference is:

{ table_name [ TABLE ] [ alias ]
  | join_table
  | LATERAL ( subquery ) [ alias ]
  | ( subquery ) [ alias ]
  | VALUES ( { expression } [, ...] ) [, ...] [ alias ]
  } 

join_table is:

table_reference [ join_type ] JOIN table_reference [ ON join_condition | USING ( column [, ...] ) ] 

join_type is:

{ INNER | CROSS | LEFT [ OUTER ] | RIGHT [ OUTER ] | FULL [ OUTER ] | LEFT SEMI | LEFT ANTI }

Parameters

Parameter

Description

table_name

The table name, specifying the table to be queried. The TABLE keyword can be used to reference a table value.

alias

Specifies an alias for a table or subquery. A subquery must specify an alias.

LATERAL (subquery)

A LATERAL subquery allows the subquery to reference columns from tables that appear earlier in the same FROM clause. Enhanced support in Spark 4.0.

(subquery)

A nested subquery. The subquery result serves as an intermediate temporary table. A subquery must specify an alias.

VALUES

A row constructor. Directly constructs row data as a data source.

join_type

The join type, including INNER, CROSS, LEFT OUTER, RIGHT OUTER, FULL OUTER, LEFT SEMI, and LEFT ANTI.

ON join_condition

The JOIN condition.

USING (column)

A shorthand for performing a JOIN using columns with the same name.

Precautions

  • The table being queried must already exist. Otherwise, an error will occur.
  • In a FROM nested subquery, the subquery must specify an alias.
  • A LATERAL subquery can reference columns from tables to its left in the same FROM clause. A regular subquery cannot reference outer columns.

Examples

Subquery as a data source:

SELECT DISTINCT name FROM (SELECT name FROM student_info   JOIN course_info ON student_info.courseId = course_info.courseId) temp; 

LATERAL subquery referencing an outer column:

SELECT * FROM orders o,   LATERAL (SELECT * FROM order_items WHERE order_id = o.id) items; 

VALUES constructing data:

SELECT * FROM VALUES (1, 'a'), (2, 'b'), (3, 'c') AS t(id, name); 

LATERAL VIEW with explode:

SELECT name, course FROM student   LATERAL VIEW EXPLODE(courses) t AS course;