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

FLATTEN: one array becomes several rows

FLATTEN is a table function. LATERAL lets it read each preceding source row and emit that row’s child values.

Understand the idea

The following sample is self-contained. OUTER preserves the empty array’s event, but its placeholder is not an actual item. Use COUNT(f.INDEX) for actual array positions rather than COUNT(*).

Key ideas in simple English
ConceptWhat it means
VALUEThe element being emitted.
INDEX / KEYArray position or object key.
PATH / THISLocation and compound value being expanded.
SEQInput-associated sequence; not a stable business identifier.
OUTER=TRUERetains a zero-row expansion with a placeholder row.

A concrete example

An event with two items produces two child rows. An empty items array normally produces none.

Remember: Choose the output grain before flattening: one row per event and one row per item are different tables.

Explain it step by step · full theory

How to apply it

The following sample is self-contained. OUTER preserves the empty array’s event, but its placeholder is not an actual item. Use COUNT(f.INDEX) for actual array positions rather than COUNT(*).

Ordering is explicit: use event_id and INDEX. RECURSIVE=TRUE explores deeper structures and PATH can start from a selected location; for known nested arrays, chained lateral calls make parent-child relationships clearer.

Words used in this lesson
VALUE
The element being emitted.
INDEX / KEY
Array position or object key.
PATH / THIS
Location and compound value being expanded.
SEQ
Input-associated sequence; not a stable business identifier.
OUTER=TRUE
Retains a zero-row expansion with a placeholder row.

See how it works

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

Open the interactive explanation
Interactive explainer1 / 3

FLATTEN: one array becomes several rows

VALUE
INDEX / KEY

Read the shape

FLATTEN is a table function. LATERAL lets it read each preceding source row and emit that row’s child values.

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

    FLATTEN is a table function. LATERAL lets it read each preceding source row and emit that row’s child values.

  2. Follow the example

    An event with two items produces two child rows. An empty items array normally produces none.

  3. Verify the result

    Choose the output grain before flattening: one row per event and one row per item are different tables.

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.

Keep the parent with an empty array

SQL · example
WITH events AS (
 SELECT PARSE_JSON(column1) AS p FROM VALUES
 ('{"id":101,"items":[{"sku":"A"},{"sku":"B"}]}'),
 ('{"id":102,"items":[]}')
)
SELECT p:id::NUMBER AS EVENT_ID, f.INDEX AS ITEM_INDEX,
 f.VALUE:sku::VARCHAR AS SKU
FROM events, LATERAL FLATTEN(INPUT=>p:items, OUTER=>TRUE) f
ORDER BY EVENT_ID, ITEM_INDEX;

What to expect and why

Returns (101,0,A), (101,1,B), and (102,NULL,NULL). Without OUTER, event 102 disappears from this result.

Check your understanding

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

An empty array produces a placeholder with OUTER. Is that a real child?

Try it yourself

Why should you avoid summing an event-level total after flattening its items?

Compare your reasoning

The parent total repeats for each child. Aggregate at the proper grain or use a separate parent table.

Official sources and curriculum

Module 07 · See module scope