
Ultimate access to all questions.
A data engineer has a Python variable table_name that they would like to use in a SQL query. They want to construct a Python code block that will run the query using table_name. They have the following incomplete code block: ____(F"SELECT customer_id, spend FROM (table_name)")
Which of the following can be used to fill in the blank to successfully complete the task?
A
spark.sql
B
spark.read
C
spark.execute
D
spark.query
E
spark.table
F
spark.run
Explanation:
The correct answer is A. spark.sql.
(table_name) will be replaced with the actual value of the Python variable table_name.spark.sql("SELECT ... FROM table_name") is the standard way to run SQL queries in PySpark.# Assuming table_name is a Python variable
table_name = "sales_data"
result = spark.sql(F"SELECT customer_id, spend FROM {table_name}")
# Assuming table_name is a Python variable
table_name = "sales_data"
result = spark.sql(F"SELECT customer_id, spend FROM {table_name}")
Note: The original code shows (table_name) in parentheses, but in an f-string, it should be {table_name}. The correct f-string syntax would be:
spark.sql(F"SELECT customer_id, spend FROM {table_name}")
spark.sql(F"SELECT customer_id, spend FROM {table_name}")