spark.sql and temp views
Mix SQL and DataFrames: register views, run spark.sql, and pass parameters safely.
On this page
Show code in
Every code block on the page follows this.
You will learn
- How to register a DataFrame as a view and query it with spark.sql
- The difference between temp views, global temp views and tables
- How to pass parameters into SQL safely
- Why SQL and DataFrame code run at the same speed
df.createOrReplaceTempView("orders") gives a DataFrame a name for this session; spark.sql("SELECT ... FROM orders") returns a new DataFrame. Both APIs compile to the same plan, so choose whichever is clearer. Pass values with parameters, spark.sql(query, args={...}), not with f-strings.Going back and forth
orders.createOrReplaceTempView("orders") top = spark.sql(""" SELECT customer, SUM(amount) AS total FROM orders GROUP BY customer """) result = top.filter(F.col("total") > 1000).orderBy(F.desc("total"))
The result of spark.sql is an ordinary DataFrame: you can keep chaining DataFrame methods, register it as another view, or write it out. Nothing runs until an action, exactly as with DataFrame code.
Views and tables
| Kind | Created with | Visible to | Lives until |
|---|---|---|---|
| Temp view | createOrReplaceTempView | This Spark session only | The session ends |
| Global temp view | createOrReplaceGlobalTempView; query as global_temp.name | All sessions in the same application | The application ends |
| Table | saveAsTable, CREATE TABLE | Everyone using the catalog | Dropped |
| Permanent view | CREATE VIEW in SQL | Everyone using the catalog | Dropped; stores only the query, not data |
A temp view stores no data. It is a name for a plan: querying it re-runs the plan, including reading the source, unless the DataFrame is cached.
Passing parameters safely
Building SQL with f-strings breaks on quotes in values and opens the door to SQL injection when values come from users or files. Spark 3.4+ supports named parameters:
# Fragile: breaks for O'Brien, and unsafe with untrusted input spark.sql(f"SELECT * FROM orders WHERE customer = '{name}'") # Safe: the value is passed separately spark.sql("SELECT * FROM orders WHERE customer = :name AND amount > :min", args={"name": name, "min": 100}) # DataFrames can be passed straight in (Spark 3.4+) spark.sql("SELECT * FROM {o} WHERE amount > 100", o=orders)
Same plan, same speed
SQL text and DataFrame calls are two front ends to the same engine. Both are parsed into a logical plan, optimised by CatalystCatalyst: Spark's query optimizer. It rewrites your DataFrame code into a faster equivalent plan before running it. Learn more → and executed the same way. explain() on either shows identical physical plans for equivalent queries. Choose by readability: SQL for analysts and long set-based logic, DataFrames when you build queries dynamically in Python (loops over columns, reusable functions, unit tests).
SQL · text
The command
spark.sql("SELECT customer, SUM(amount) AS revenue FROM orders WHERE amount > 0 GROUP BY customer")
SELECT customer, SUM(amount) AS revenue FROM orders WHERE amount > 0 GROUP BY customer
DataFrame · calls
The command
orders.filter("amount > 0").groupBy("customer").agg(F.sum("amount").alias("revenue"))
Catalyst · optimise
Mixing them well
F.expr("CASE WHEN ... END")anddf.selectExpr("amount * 1.18 AS gross")embed SQL expressions inside DataFrame code.df.filter("amount > 100")accepts a SQL condition string.- Register intermediate results as views to break a long SQL pipeline into readable steps, or use CTEs (
WITH ... AS) inside one query. - Use
spark.table("db.orders")to read a catalog table as a DataFrame.
Common mistakes
f-strings for values
Expecting a temp view to be visible in another notebook or job
Believing SQL is faster (or slower) than DataFrames
Treating a view as cached
Key takeaways
- createOrReplaceTempView names a DataFrame for SQL in one session.
- spark.sql returns a lazy DataFrame.
- Pass values with args, never with f-strings.
- SQL and DataFrames compile to the same plan and run at the same speed.
Check yourself
3 questions1. Where is a temp view visible?
Show the answer
Only in the session that created it. Temp views are session-scoped; global temp views are application-scoped.
2. Does a temp view store data?
Show the answer
No, it names a plan that is re-run when queried. A view is a name for a query plan; cache the DataFrame if you need it kept.
3. Which is the safe way to filter by a user-supplied name?
Show the answer
spark.sql with args={"name": name}. Parameters are passed separately from the SQL text, so quotes cannot break or inject into the query.
Practice it
Interview problems that use this: write the PySpark, run it, and get graded on hidden tests.
Keep going
Up next · lesson 15 of 35 · 3 min readrow_number / rank
Number and rank rows inside a window, and how the three ranking functions treat ties.
Related lessons
Catalyst and physical plansPySpark functions · 4 min read
selectSpark internals · 3 min read
SQL pipe syntax
Previous: dropDuplicates
Primary sources: SparkSession.sql · DataFrame.createOrReplaceTempView