What is a Nested Field in BigQuery ?

Google BigQuery is a highly scalable, serverless data warehouse designed to enable fast SQL queries using the processing power of Google's infrastructure. One of its powerful features is the ability to handle complex data structures, particularly nested fields. Understanding nested fields in BigQuery is crucial for efficiently managing and querying semistructured data.

Introduction to Nested Fields in BigQuery

In traditional relational databases, data is stored in tables with a flat schema, where each row represents a record and each column represents an attribute of that record. However, realworld data is often hierarchical or semistructured, containing nested or repeated elements. Nested fields in BigQuery allow you to represent this complexity directly within your tables, providing a more natural and efficient way to store and query hierarchical data. Nested fields are part of BigQuery's support for complex data types, which includes:

  • STRUCTs: These are complex data types that group multiple fields together, similar to a row in a relational database.
  • ARRAYs: These are ordered lists of elements, all of the same type.

STRUCTs (Records)

A STRUCT (also known as a RECORD) is a complex data type that groups related fields together. Each field within a STRUCT can have its own data type, and a STRUCT can even contain other STRUCTs, allowing for deeply nested hierarchies. Example Consider a dataset of customer orders, where each order has multiple attributes such as order ID, customer details, and a list of items. A flat schema might look like this:

order_id customer_id customer_name item_id item_name item_quantity
1 101 Alice A1 Widget 2
1 101 Alice A2 Gizmo 1
2 102 Bob A1 Widget 1

Using a STRUCT, this data can be represented more naturally:

  order_id INT64,
  customer STRUCT<customer_id INT64, customer_name STRING>,
  items ARRAY<STRUCT<item_id STRING, item_name STRING, item_quantity INT64>>

This schema represents the same data but in a more organized manner. Each order now contains a nested customer record and an array of item records.

ARRAYs (Repeated Fields)

An ARRAY is an ordered list of elements, all of the same type. ARRAYs allow for the storage of repeated data within a single field, which is especially useful for lists of items, tags, or other collections. Example Continuing with the orders example, the `items` field is defined as an ARRAY of STRUCTs. This allows each order to include multiple items:

INSERT INTO orders (order_id, customer, items)
  (1, STRUCT(101, 'Alice'), [STRUCT('A1', 'Widget', 2), STRUCT('A2', 'Gizmo', 1)]),
  (2, STRUCT(102, 'Bob'), [STRUCT('A1', 'Widget', 1)]);

Querying Nested Fields

BigQuery provides powerful capabilities for querying nested and repeated data. You can use standard SQL syntax to access and manipulate these complex structures. Flattening Nested Data To flatten nested data for analysis, you can use the `UNNEST` function, which explodes an ARRAY into a set of rows:

  UNNEST(items) AS item;

This query returns a flat table by unnesting the `items` array, effectively replicating the original flat schema. Accessing Nested Fields You can access individual fields within a STRUCT using dot notation:

  items[OFFSET(0)].item_name AS first_item_name

This query retrieves the order ID, customer name, and the name of the first item in the order.

Advantages of Nested Fields

Nested fields offer several advantages:

  • Efficiency: Nested schemas reduce data redundancy and improve storage efficiency by grouping related fields together.
  • Natural Representation: They allow for a more natural representation of hierarchical and semistructured data, closely mirroring realworld entities and relationships.
  • Query Performance: By leveraging nested fields, queries can be optimized to read only the necessary parts of the data, improving performance.

Best Practices

When designing schemas with nested fields in BigQuery, consider the following best practices:

  • Use Nested Fields Appropriately: Only use nested fields when they provide a clear benefit in terms of data organization and query performance. Overusing nested fields can complicate queries and schema management.
  • Design for Query Patterns: Think about how the data will be queried. Design your schema to optimize for the most common query patterns, minimizing the need for complex joins and transformations.
  • Balance Normalization and Denormalization: While nested fields can reduce the need for joins, overly denormalized schemas can become unwieldy. Strive for a balance that simplifies queries without sacrificing data integrity or performance.


Nested fields in BigQuery provide a powerful way to handle complex, hierarchical data structures. By using STRUCTs and ARRAYs, you can create schemas that more naturally represent realworld data, improve storage efficiency, and optimize query performance. Understanding how to design, query, and manage nested fields is essential for making the most of BigQuery's capabilities, enabling you to handle largescale data with greater flexibility and efficiency.