Module 07 · Lesson 5 of 9Detailed study · about 12 min

Nested arrays: flatten one relationship at a time

For an array inside an array element, flatten the parent array first and feed its VALUE into the next FLATTEN.

Understand the idea

This query does not need the file lab. Each tag is tied to the item whose VALUE supplied it. Empty tag arrays are omitted here; use OUTER on that expansion if preserving untagged items is your goal.

Key ideas in simple English
ConceptWhat it means
First expansionEvent → items.
Second expansionItem → tags.
Parent keyCarry event_id through both steps.
Item indexPreserves the item relationship within this payload.

A concrete example

Item A has sale and gift tags; item B has new. The result has three tag rows.

Remember: Two independent sibling arrays can multiply each other. Chain related arrays deliberately.

Explain it step by step · full theory

How to apply it

This query does not need the file lab. Each tag is tied to the item whose VALUE supplied it. Empty tag arrays are omitted here; use OUTER on that expansion if preserving untagged items is your goal.

Array position identifies an element within this event version, not a permanent product key across edits. Prefer producer-supplied child IDs when available. For sibling arrays that represent aligned positions, join on INDEX only when the source contract guarantees that alignment.

Words used in this lesson
First expansion
Event → items.
Second expansion
Item → tags.
Parent key
Carry event_id through both steps.
Item index
Preserves the item relationship within this payload.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 3

Nested arrays: flatten one relationship at a time

First expansion
Second expansion

Read the shape

For an array inside an array element, flatten the parent array first and feed its VALUE into the next FLATTEN.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Read the shape

    For an array inside an array element, flatten the parent array first and feed its VALUE into the next FLATTEN.

  2. Follow the example

    Item A has sale and gift tags; item B has new. The result has three tag rows.

  3. Verify the result

    Two independent sibling arrays can multiply each other. Chain related arrays deliberately.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Manual learning examples; nothing runs on this website. Standalone WITH/SELECT examples supply their own input. Table and file exercises require the first lesson’s L2_OBJECTS_LAB.M7_LAB setup and an authorized warehouse. Select that schema in every session. The final lab requires the supplied events.json to be loaded first.

Expand items, then each item’s tags

SQL · example
WITH e AS (SELECT PARSE_JSON(
 '{"id":101,"items":[{"sku":"A","tags":["sale","gift"]},{"sku":"B","tags":["new"]}]}') AS p)
SELECT p:id::NUMBER AS EVENT_ID, i.INDEX AS ITEM_INDEX,
 i.VALUE:sku::VARCHAR AS SKU, t.INDEX AS TAG_INDEX,
 t.VALUE::VARCHAR AS TAG
FROM e,
 LATERAL FLATTEN(INPUT=>p:items) i,
 LATERAL FLATTEN(INPUT=>i.VALUE:tags) t
ORDER BY ITEM_INDEX, TAG_INDEX;

What to expect and why

The three rows are A/sale, A/gift and B/new. Item A appears twice because the output grain is one tag, not one item.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

Two independent arrays contain 2 and 3 elements. Flattening both per parent can produce how many rows?

Try it yourself

How do you keep item quantity from being counted twice when an item has two tags?

Compare your reasoning

Keep item quantities at item grain. Store tags separately or aggregate tags before joining them to item totals.

Official sources and curriculum

Module 07 · See module scope