SQL on Files, Temporary Views, and Tables
Databricks Spark Developer Associate · Lesson 7 of 18 · 48:58
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: SQL directly on files, temporary and global temporary views, persistent tables with saveAsTable, and partitioning, bucketing, and sorting. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
SQL, without a load job
Coralpeak's analysts prefer SQL to Python, and they want to query yesterday's files without waiting for a load job. They also need tables that are still there tomorrow, laid out so that the common queries read as little data as possible. This lesson covers SQL directly on files, temporary views, persistent tables, and partitioning, bucketing, and sorting.
SQL directly on files
Spark SQL can query a file or a folder directly, without loading it into a DataFrame first: in the FROM clause, you write the format, a dot, and the path between backticks. This works with parquet, json, csv, orc, and text files, and with delta, for the folder of a Delta table. The sql method of the session runs the query and returns a DataFrame, so you can mix SQL and Python freely. But a direct query uses the default options of the format, so for CSV files with a header, or a schema to enforce, a view over a reader with options is the better choice.
Temporary views
createOrReplaceTempView registers a DataFrame under a name, so that SQL queries can use it like a table. A temporary view is not stored anywhere: it is a name for the plan of the DataFrame, and Spark computes it again each time a query uses it. It belongs to the SparkSession that created it, and disappears when that session ends. createGlobalTempView shares a view across the sessions of the same application, until the application stops, and you query it with the global_temp prefix. And createTempView fails if the name is already taken, while the versions with or replace overwrite it.
Persistent tables
saveAsTable writes the data and registers the table in the catalog, so the table survives restarts, and any session can read it by name, with the table method of the session or with SQL. Without a path option, the table is managed: its files live in the warehouse location, and dropping the table deletes them. With a path option, the table is external: dropping it removes only its definition, and the files stay where they are. And the save mode still applies: by default, saveAsTable fails if the table exists, while append and overwrite add to it or replace it.
Partitioning, bucketing, and sorting
When you save a table, partitionBy splits the files into one folder per value, which works with save and with saveAsTable, and suits columns with few values, like a date. bucketBy hashes a column into a fixed number of buckets, which suits columns with many values, like a player identifier, and it can avoid a shuffle when two tables are bucketed the same way on the join key. sortBy sorts the rows inside each bucket, which speeds up joins and range filters on that column. bucketBy and sortBy only work with saveAsTable, not with a save to a path, since the bucket layout is kept in the metadata of the table. In SQL, the same layout uses partitioned by, clustered by into a number of buckets, and sorted by, in a create table statement.
Recap
Let's recap. Spark SQL queries files directly, with the format and the path between backticks, for parquet, json, csv, orc, text, and delta. A temporary view names a DataFrame for SQL, and ends with its session, while a global temporary view stays in global_temp until the application stops. saveAsTable writes the data and registers a table that survives restarts, managed without a path, and external with one. partitionBy makes a folder per value of a column with few values, and works with save and saveAsTable. And bucketBy and sortBy organize columns with many values, and need saveAsTable.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
An analyst wants to query yesterday’s JSON event files with SQL, without a table or a DataFrame first. Which query works?
- SELECT * FROM '/data/events/2026-11-26/'
- SELECT * FROM events.json
- LOAD JSON '/data/events/2026-11-26/'
- SELECT * FROM json.`/data/events/2026-11-26/`
Show the answer
SELECT * FROM json.`/data/events/2026-11-26/`
The format, a dot, and the path between backticks let Spark SQL query the files directly. “SELECT * FROM '/data/events/2026-11-26/'” is wrong, since a quoted path alone names no format, and is not a table. “SELECT * FROM events.json” is wrong, since it reads as a table named json, in a database named events. And “LOAD JSON '/data/events/2026-11-26/'” is wrong, since it is not a Spark SQL statement that returns rows.
A job registers the daily totals with createOrReplaceTempView, and a new session, started the next morning, cannot find the view. Why?
- Views must be cached before others can find them
- A temporary view ends with the session that created it
- The view was saved in the global_temp database
- Views can only be read from Python code
Show the answer
A temporary view ends with the session that created it
A temporary view belongs to its session, and disappears when that session ends, so the next session needs a table instead. “Views must be cached before others can find them” is wrong, since a view does not need a cache to be found. “The view was saved in the global_temp database” is wrong, since createOrReplaceTempView makes a session view, and even a global view ends with its application. And “Views can only be read from Python code” is wrong, since views exist precisely so that SQL can query them.
Coralpeak writes the purchases with bucketBy on the player identifier, then calls save(path). What happens?
- The files in the folder are bucketed
- Spark ignores bucketBy and writes plain files
- The write fails: bucketBy needs saveAsTable
- Spark writes one folder for each player
Show the answer
The write fails: bucketBy needs saveAsTable
Bucketing and sorting apply only to persistent tables, so bucketBy requires saveAsTable, and save raises an error. “The files in the folder are bucketed” is wrong, since a plain path has no place to keep the bucket layout. “Spark ignores bucketBy and writes plain files” is wrong, since Spark does not silently drop the option, it refuses the write. And “Spark writes one folder for each player” is what partitionBy does, which would create millions of tiny folders here.