Skip to main content
PYSPARK • LESSON 123

Mixing DataFrame API & SQL Syntax

How do we seamlessly combine SQL queries with downstream DataFrame filter operations in a unified pipeline?

Advanced2 Minutes650 XP
🤔 THE QUESTION

How do we seamlessly combine SQL queries with downstream DataFrame filter operations in a unified pipeline?

💡 WHAT IS IT?

Querying temporary views with spark.sql() and immediately chaining DataFrame methods (.filter, .select) on the result.

🎯 WHAT IS IT USED FOR?

Leveraging SQL for complex aggregations and DataFrame APIs for programmatic downstream transformations.

💻 EXAMPLE
df.createOrReplaceTempView("orders")
gold_df = spark.sql("""
    SELECT customer_id, SUM(amount) AS total_spend
    FROM orders
    GROUP BY customer_id
""").filter("total_spend > 5000")

🎯 Mission Objectives

Practice typing production-grade PySpark code for Mixing DataFrame API & SQL Syntax.

  • Register orders temporary view
  • Execute SQL aggregation query
  • Chain DataFrame filter on SQL result