
Answer-first summary for fast verification
Answer: ```sql SELECT flatten(employee_id) as employee_ids FROM df_employees ```
- **`flatten()`** is designed to convert an **array of arrays into a single array**, without changing the row count. - **`explode()`** increases the number of rows, which violates the requirement. - **C** flattens correctly but then explodes, which is unnecessary and incorrect. - **D** explodes the outer array, not the inner arrays. - **E** is false because only **A** meets all conditions. 📌 **Exam Tip:** - Use **`flatten()`** → when dealing with `ARRAY<ARRAY<T>>` and keeping row count - Use **`explode()`** → when turning array elements into multiple rows
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
You are working with a Spark SQL table df_employees that contains the following schema:
employee_id ARRAY<ARRAY<INT>>,
name STRING,
department STRING,
salary DOUBLE
employee_id ARRAY<ARRAY<INT>>,
name STRING,
department STRING,
salary DOUBLE
Each row in employee_id contains multiple arrays of employee IDs (for example: [[101,102],[103,104]]).
Your task is to convert the employee_id column into a single array of integers per row, without increasing the number of rows, using Spark SQL.
Which of the following queries correctly accomplishes this task?
A
SELECT flatten(employee_id) as employee_ids FROM df_employees
SELECT flatten(employee_id) as employee_ids FROM df_employees
B
SELECT explode(employee_id) AS employee_id
FROM df_employees
SELECT explode(employee_id) AS employee_id
FROM df_employees
C
SELECT explode(flatten(employee_id)) AS employee_id
FROM df_employees
SELECT explode(flatten(employee_id)) AS employee_id
FROM df_employees
D
SELECT employee_id
FROM df_employees
LATERAL VIEW explode(employee_id) e AS employee_id
SELECT employee_id
FROM df_employees
LATERAL VIEW explode(employee_id) e AS employee_id