
Answer-first summary for fast verification
Answer: Cmd 1 will succeed and Cmd 2 will fail. countries_af will be a Python variable containing a list of strings.
Cmd 1 successfully creates a Python list `countries_af` from the `geo_lookup` table for countries in Africa. Cmd 2 attempts to use this list in a SQL query to create a view `sales_af` but fails because SQL in Databricks cannot directly access Python variables without specific parameterization. The SQL engine looks for a table or view named `countries_af`, which does not exist, leading to the failure of Cmd 2. The correct understanding is that Cmd 1 succeeds and Cmd 2 fails, with `countries_af` being a Python list of strings, not a PySpark DataFrame or a registered view.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
A junior data engineer is working with Databricks notebook language interoperability. The goal is to create a view showing all sales from African countries listed in the geo_lookup table. The current database contains only two tables: geo_lookup and sales.
The following code is executed:
%python
countries_af = [x[0] for x in
spark.table("geo_lookup").filter("continent='AF'").select("country").collect()]
%python
countries_af = [x[0] for x in
spark.table("geo_lookup").filter("continent='AF'").select("country").collect()]
%sql
CREATE VIEW sales_af AS
SELECT *
FROM sales
WHERE country IN (countries_af)
AND continent = 'AF'
%sql
CREATE VIEW sales_af AS
SELECT *
FROM sales
WHERE country IN (countries_af)
AND continent = 'AF'
What will be the result of executing these command cells sequentially in an interactive notebook?
A
Both commands will succeed. Executing SHOW TABLES will show that countries_af and sales_af have been registered as views.
B
Cmd 1 will succeed. Cmd 2 will search all accessible databases for a table or view named countries_af: if this entity exists, Cmd 2 will succeed.
C
Cmd 1 will succeed and Cmd 2 will fail. countries_af will be a Python variable representing a PySpark DataFrame.
D
Cmd 1 will succeed and Cmd 2 will fail. countries_af will be a Python variable containing a list of strings.