Module 04 · Lesson 13 of 16Detailed study · about 18 min

GENERATE rows and evaluate lateral queries

GENERATOR supplies synthetic rows. A lateral query can refer to a preceding input row and return zero, one or several related rows for it.

Understand the idea

Sometimes the missing input is a calendar or a small sequence, not a stored business table. GENERATOR creates rows; expressions in SELECT create their values. SEQ4 can have gaps, so use ROW_NUMBER when a contiguous number sequence matters.

Key ideas in simple English
ConceptWhat it means
GENERATORA table function that supplies a requested number of rows.
ROW_NUMBERCan turn generated rows into a contiguous sequence.
LATERALLets the right-hand expression use columns from the current left row.

A concrete example

Returns January 1, 2, 3 and 4 of 2026. ROW_NUMBER supplies contiguous positions.

Remember: LATERAL allows the right-hand expression to depend on the preceding row. It can change both values and row counts.

Explain it step by step · full theory

Let us understand the idea

Sometimes the missing input is a calendar or a small sequence, not a stored business table. GENERATOR creates rows; expressions in SELECT create their values. SEQ4 can have gaps, so use ROW_NUMBER when a contiguous number sequence matters.

A lateral operation is useful when each input row has a different related result. Unlike an independent cross join, its right side can refer to the current left row. If that right side produces no rows, the outer row is not automatically preserved.

Reason through the SQL

The date query builds four consecutive days by adding offsets 0 through 3. This is a small deterministic calendar, not a performance test. Use a fixed ROWCOUNT for a predictable number of rows.

The lateral example calculates discounted amounts separately for each order. Array expansion with LATERAL FLATTEN is another use, covered in the later semi-structured-data module. Do not assume every lateral table function supports every outer-join or ON-clause form.

Words used in this lesson
GENERATOR
A table function that supplies a requested number of rows.
ROW_NUMBER
Can turn generated rows into a contiguous sequence.
LATERAL
Lets the right-hand expression use columns from the current left row.

See how it works

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

Open the interactive explanation
Interactive explainer1 / 3

GENERATE rows and evaluate lateral queries

Input rows
Business question

Begin with the question

GENERATOR supplies synthetic rows. A lateral query can refer to a preceding input row and return zero, one or several related rows for it.

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. Begin with the question

    GENERATOR supplies synthetic rows. A lateral query can refer to a preceding input row and return zero, one or several related rows for it.

  2. Choose the operation

    Sometimes the missing input is a calendar or a small sequence, not a stored business table. GENERATOR creates rows; expressions in SELECT create their values. SEQ4 can have gaps, so use ROW_NUMBER when a contiguous number sequence matters.

  3. Check the meaning

    LATERAL allows the right-hand expression to depend on the preceding row. It can change both values and row counts.

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

Read first, then run manually in an authorized Snowflake learning session with a warehouse. Each WITH/VALUES example includes its own inputs and creates no stored tables. Expected results are illustrative, not live execution.

Build four calendar dates

SQL · example
SELECT DATEADD(day, ROW_NUMBER() OVER (ORDER BY SEQ4()) - 1,
 DATE '2026-01-01')::DATE AS DAY
FROM TABLE(GENERATOR(ROWCOUNT => 4)) ORDER BY DAY;

What to expect and why

Returns January 1, 2, 3 and 4 of 2026. ROW_NUMBER supplies contiguous positions.

Use the current order inside a lateral query

SQL · example
WITH orders AS (SELECT COLUMN1 AS ID, COLUMN2 AS AMOUNT FROM VALUES (1,100),(2,50))
SELECT o.ID, d.DISCOUNTED_AMOUNT
FROM orders o, LATERAL (SELECT o.AMOUNT * 0.90 AS DISCOUNTED_AMOUNT) d
ORDER BY o.ID;

What to expect and why

The right-hand calculation reads o.AMOUNT for each input order.

Expected result for the supplied input (not live output)
IDDISCOUNTED_AMOUNT
190
245

Check your understanding

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

What makes the second query lateral?

Try it yourself

Extend the generated calendar to seven days without using a stored table.

Compare your reasoning

Change ROWCOUNT => 4 to ROWCOUNT => 7. The dates run from January 1 through January 7, 2026.

Official sources and curriculum

Module 04 · See module scope