The relationship between ARRAY_AGG and UNNEST

title: "The relationship between ARRAY_AGG and UNNEST" seoTitle: "BigQuery ARRAY_AGG vs UNNEST: Opposite Operations Explained" seoDescription: "ARRAY_AGG packs rows into an ARRAY and UNNEST unpacks an ARRAY back into rows — they are inverse operations in BigQuery." datePublished: Sat Jun 08 2024 18:26:00 GMT+0000 (Coordinated Universal Time) cuid: clx6g4uh900000ajm1u87d5dg slug: the-relationship-between-arrayagg-and-unnest cover: https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/VhDgReMsz8w/upload/910ca1350e91fa8b0c28ce16e9acd9c9.jpeg tags: databases, google-cloud, sql, bigquery, data-engineering
If you're working with nested data in BigQuery, you've might've seen UNNEST, which helps 'unpack' arrays into individual rows.
But there's also ARRAY_AGG, which, if you haven't encountered it before, which takes all rows for your GROUP BY bucket and creates an ARRAY out of them.
So, in essence, ARRAY_AGG and UNNEST are doing the exact opposite of each other.
Check my previous posts on the topic:
-
Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.
Enjoyed this? Here are some related articles you might find useful:





