Module 10 · Lesson 3 of 8Detailed study · about 20 min

Return rows with a SQL UDTF

A table function returns a table-shaped result. Use TABLE(...) to place it in a query’s FROM clause.

Understand the idea

A scalar calculator returns one value. A table function can return several useful rows, such as a base-price row and a discounted-price row for one input.

Key ideas in simple English
ConceptWhat it means
RETURNS TABLEDeclares the output columns.
TABLE(function(...))Makes function output queryable as rows.
UDTFA table function defined by you.

A concrete example

Two rows: Ten percent off / 90.00, then Base / 100.00. No persistent table is created by the SELECT.

Remember: A UDTF declares a row-shaped contract and can return multiple rows.

Explain it step by step · full theory

Understand the idea

A scalar calculator returns one value. A table function can return several useful rows, such as a base-price row and a discounted-price row for one input.

The number and types of returned columns must match the declaration. A function returning two rows per input can multiply downstream row counts, so inspect the output grain before joining it.

Follow the example

This example uses UNION ALL to keep both labeled rows even when their amounts happen to match.

Words used in this lesson
RETURNS TABLE
Declares the output columns.
TABLE(function(...))
Makes function output queryable as rows.
UDTF
A table function defined by you.

See how it works

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

Open the interactive explanation
Interactive explainer1 / 3

Return rows with a SQL UDTF

Input
Business rule

Start with the requirement

A table function returns a table-shaped result. Use TABLE(...) to place it in a query’s FROM clause.

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. Start with the requirement

    A table function returns a table-shaped result. Use TABLE(...) to place it in a query’s FROM clause.

  2. Apply the rule

    A scalar calculator returns one value. A table function can return several useful rows, such as a base-price row and a discounted-price row for one input.

  3. Explain the result

    A UDTF declares a row-shaped contract and can return multiple rows.

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 practice in an authorized Snowflake learning account. Run lesson 1 setup first and select L2_OBJECTS_LAB.M10_LAB and your authorized warehouse in each session. Use fresh object names; do not replace existing objects. Read whole scripting blocks before running them. The website does not execute commands.

Return two pricing choices

SQL · example
CREATE FUNCTION M10_PRICE_OPTIONS(AMOUNT NUMBER(12,2))
RETURNS TABLE (OPTION_NAME VARCHAR, PRICE NUMBER(12,2))
LANGUAGE SQL AS $$
 SELECT 'Base', AMOUNT
 UNION ALL SELECT 'Ten percent off', ROUND(AMOUNT*0.90,2)
$$;
SELECT * FROM TABLE(M10_PRICE_OPTIONS(100)) ORDER BY PRICE;

What to expect and why

Two rows: Ten percent off / 90.00, then Base / 100.00. No persistent table is created by the SELECT.

Check your understanding

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

Why use TABLE around this function call?

Try it yourself

How many rows appear if the input is 0?

Compare your reasoning

Two labeled rows, both priced at 0. UNION ALL preserves both choices.

Official sources and curriculum

Module 10 · See module scope