
Answer-first summary for fast verification
Answer: The sql() operation should be accessed via the spark variable rather than DataFrame storesDF.
The error in the code block is due to the incorrect use of the `sql()` method on the DataFrame `storesDF`. The `sql()` method is not a DataFrame operation; it is a method of the `SparkSession` object, typically accessed via the `spark` variable. After creating a temporary view of the DataFrame with `createOrReplaceTempView()`, the correct way to execute a SQL query is by using `spark.sql()`. Therefore, the correct answer is B, as it accurately identifies the misuse of the `sql()` method on the DataFrame and correctly states that it should be accessed via the `spark` variable instead. The other options either misrepresent the functionality of `createOrReplaceTempView()` or incorrectly suggest that SQL cannot be used for this purpose.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
The code block shown below contains an error. The code block is intended to use SQL to return a new DataFrame containing column storeId and column managerName from a table created from DataFrame storesDF. Identify the error.
Code block:
storesDF.createOrReplaceTempView("stores")
storesDF.sql("SELECT storeId, managerName FROM stores")
storesDF.createOrReplaceTempView("stores")
storesDF.sql("SELECT storeId, managerName FROM stores")
A
The createOrReplaceTempView() operation does not make a Dataframe accessible via SQL.
B
The sql() operation should be accessed via the spark variable rather than DataFrame storesDF.
C
There is the sql() operation in DataFrame storesDF. The operation query() should be used instead.
D
This cannot be accomplished using SQL – the DataFrame API should be used instead.
E
The createOrReplaceTempView() operation should be accessed via the spark variable rather than DataFrame storesDF.