In Spark Notebooks in Microsoft Fabric, how can you execute a Spark SQL query within a PySpark notebook cell while keeping the language as PySpark?

Prepare for the DP-700 Microsoft Fabric Data Engineer Exam with flashcards and multiple choice questions. Study with hints and explanations, and ensure success on your certification exam!

Multiple Choice

In Spark Notebooks in Microsoft Fabric, how can you execute a Spark SQL query within a PySpark notebook cell while keeping the language as PySpark?

Explanation:
Use the SparkSession’s sql method to run Spark SQL inside a PySpark cell. In PySpark you already have access to the active SparkSession named spark, so you can pass a SQL string to spark.sql(...) and get back a DataFrame. This lets you mix SQL with Python code without changing the cell language. For example, you can do: df = spark.sql("SELECT name, COUNT(*) FROM people GROUP BY name") df.show() You can also build the query in a Python variable and pass it to spark.sql, then continue using PySpark operations on the resulting DataFrame. Other options don’t fit because they switch the cell to SQL (losing PySpark language) or change the notebook-wide language, or rely on an additional utility that isn’t the standard way to execute SQL within a PySpark cell.

Use the SparkSession’s sql method to run Spark SQL inside a PySpark cell. In PySpark you already have access to the active SparkSession named spark, so you can pass a SQL string to spark.sql(...) and get back a DataFrame. This lets you mix SQL with Python code without changing the cell language. For example, you can do:

df = spark.sql("SELECT name, COUNT(*) FROM people GROUP BY name")

df.show()

You can also build the query in a Python variable and pass it to spark.sql, then continue using PySpark operations on the resulting DataFrame.

Other options don’t fit because they switch the cell to SQL (losing PySpark language) or change the notebook-wide language, or rely on an additional utility that isn’t the standard way to execute SQL within a PySpark cell.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy