Category: Semi-Structured Data / Table Functions
Platform Support:
✅ Snowflake (FLATTEN) | ✅ BigQuery (UNNEST) | ✅ Databricks (EXPLODE)
Description
Expands arrays or nested structures into multiple rows. This is essential for working with JSON, arrays, and nested data formats common in modern data warehouses. Each platform has its own syntax but achieves the same goal.
Syntax by Platform
Snowflake:
BigQuery:
Databricks:
Platform-Specific Notes
Snowflake:
FLATTEN works with VARIANT, ARRAY, and OBJECT types
Supports recursive flattening with
RECURSIVE => TRUEReturns structured data with
SEQ,KEY,PATH,INDEX,VALUE,THISCan handle deeply nested JSON
BigQuery:
UNNEST has simpler syntax
Works with ARRAY and STRUCT types
Can unnest multiple arrays in same query with cross join behavior
Supports WITH OFFSET for array indices
Databricks:
EXPLODE is standard Spark SQL
Can also use
inline()for structsEXPLODE_OUTERkeeps NULL valuesCompatible with Hive/Spark ecosystems
Example 1: Expand Order Items
Snowflake:
BigQuery:
Databricks:
Sample Data (orders table with nested items):
order_id | customer_name | items |
|---|---|---|
101 | John Smith | [{"product_name":"Laptop","quantity":1,"price":999.99},{"product_name":"Mouse","quantity":2,"price":29.99}] |
102 | Maria Garcia | [{"product_name":"Keyboard","quantity":1,"price":89.99}] |
103 | Sarah Johnson | [{"product_name":"Monitor","quantity":2,"price":349.99},{"product_name":"Cable","quantity":3,"price":12.99}] |
Result (all platforms):
order_id | product_name | quantity | price |
|---|---|---|---|
101 | Laptop | 1 | 999.99 |
101 | Mouse | 2 | 29.99 |
102 | Keyboard | 1 | 89.99 |
103 | Monitor | 2 | 349.99 |
103 | Cable | 3 | 12.99 |
Example 2: Flatten with Multiple Levels
Snowflake (Recursive Flatten):
BigQuery:
Sample Data (user_data table):
user_id | categories |
|---|---|
1 | [{"name":"Tech","tags":["AI","Cloud"]},{"name":"Sports","tags":["Football"]}] |
2 | [{"name":"Music","tags":["Rock","Jazz","Blues"]}] |
Result:
user_id | category_name | tag |
|---|---|---|
1 | Tech | AI |
1 | Tech | Cloud |
1 | Sports | Football |
2 | Music | Rock |
2 | Music | Jazz |
2 | Music | Blues |
Use Cases
Processing event logs with nested attributes
Analyzing e-commerce order data with line items
Working with API responses in JSON format
Transforming denormalized data into relational format
Expanding user behavior arrays (clicks, views, purchases)
Performance Considerations
Flattening large arrays can significantly increase row count - use filters before flattening when possible
In Snowflake, FLATTEN creates implicit joins which can be costly
In BigQuery, UNNEST is optimized for columnar storage
Consider materialized views for frequently flattened data





