
Answer-first summary for fast verification
Answer: order_date = dbutils.widgets.get('widget_order_date')spark.sql(f'SELECT * FROM sales WHERE orderDate = \'{order_date}\'')
The correct approach involves using an f-string to dynamically insert the `order_date` variable into the SQL query. The f-string allows embedding Python expressions directly into the string, and the variable is enclosed in single quotes to ensure it's treated as a string value within the SQL query. This method effectively retrieves the widget value and uses it to filter the data dynamically based on user input.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
In a Python notebook cell, which statement correctly reads a notebook widget and passes the Python variable to a SQL statement?
A
order_date = dbutils.widgets.get('widget_order_date')spark.sql('SELECT * FROM sales WHERE orderDate = order_date')
B
order_date = dbutils.widgets.get('widget_order_date')spark.sql(f'SELECT * FROM sales WHERE orderDate = '{order_date}'')
C
order_date = dbutils.widgets.get('widget_order_date')spark.sql(f'SELECT * FROM sales WHERE orderDate = '${order_date}'')
D
order_date = dbutils.widgets.get('widget_order_date')spark.sql(f'SELECT * FROM sales WHERE orderDate = 'f{order_date}'')
E
order_date = dbutils.widgets.get('widget_order_date')spark.sql(f'SELECT * FROM sales WHERE orderDate = 'order_date'')
No comments yet.