Explain the UNNEST operator in BigQuery and how it is used to flatten ARRAYs or STRUCTs.

Quick Overview

Asks how the UNNEST operator in BigQuery flattens repeated ARRAY data into rows and how it applies to arrays of STRUCTs. Tests correlated joins, row-count effects, handling of empty arrays, and the difference between flattening arrays and reading struct fields.

Explain the UNNEST operator in BigQuery and how it is used to flatten ARRAYs or STRUCTs.

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the `UNNEST` operator in BigQuery and how it is used to flatten `ARRAY`s or `STRUCT`s. ```hint Think about row counts Picture an `orders` table with an `items` array column. After flattening, how many rows does each order become, and what happens to an order whose array is empty? ``` ```hint Arrays versus structs Ask which of the two types actually holds repeated values. Only one of them needs to become extra rows. ``` ### What a Strong Answer Covers - What `UNNEST` returns for an array of scalars and for an array of structs - How the correlated join (comma or `CROSS JOIN`) ties each element to its parent row, and what that does to row counts - The difference between flattening an array and reading a struct's fields with dot notation - What happens to parent rows whose array is empty or NULL, and how to keep them - Common patterns such as filtering on array members, keeping element positions, and aggregating back to one row per parent ### Follow-up Questions - How would you count orders that contain at least one item with `qty > 1` without inflating the order count? - How do you keep the original order of the elements after flattening? - What happens when you `UNNEST` two different array columns of the same row in one `FROM` clause? - How would you flatten a nested array, such as products inside hits inside sessions?

Overview: Asks how the UNNEST operator in BigQuery flattens repeated ARRAY data into rows and how it applies to arrays of STRUCTs. Tests correlated joins, row-count effects, handling of empty arrays, and the difference between flattening arrays and reading struct fields.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the UNNEST operator in BigQuery and how it is used to flatten ARRAYs or STRUCTs.

What a Strong Answer Covers Guidance

  • What UNNEST returns for an array of scalars and for an array of structs
  • How the correlated join (comma or CROSS JOIN ) ties each element to its parent row, and what that does to row counts
  • The difference between flattening an array and reading a struct's fields with dot notation
  • What happens to parent rows whose array is empty or NULL, and how to keep them
  • Common patterns such as filtering on array members, keeping element positions, and aggregating back to one row per parent

Follow-up Questions Guidance

  • How would you count orders that contain at least one item with qty > 1 without inflating the order count?
  • How do you keep the original order of the elements after flattening?
  • What happens when you UNNEST two different array columns of the same row in one FROM clause?
  • How would you flatten a nested array, such as products inside hits inside sessions?