Module 06 · Lesson 5 of 10Detailed study · about 12 min

Unload rows into files and verify the export

COPY INTO a location exports a query result. Choose a separate output path, format and retry strategy.

Understand the idea

The internal stage owner can run this lab. A separate exporter needs source SELECT, warehouse/database/schema USAGE and WRITE on an internal stage (grant READ before WRITE). External export requires stage USAGE and approved cloud write permissions from Module 05.

Key ideas in simple English
ConceptWhat it means
COPY INTO @stage/pathWrites files instead of table rows.
HEADERInclude CSV column names.
SINGLEOne output file; useful for tiny exports.
INCLUDE_QUERY_IDUnique query identifier in parallel output names.

A concrete example

Export the two orders to a new path, then read the generated CSV: count 2, total 55.

Remember: An export path should identify a batch. OVERWRITE does not guarantee removal of unrelated old files.

Explain it step by step · full theory

How to apply it

The internal stage owner can run this lab. A separate exporter needs source SELECT, warehouse/database/schema USAGE and WRITE on an internal stage (grant READ before WRITE). External export requires stage USAGE and approved cloud write permissions from Module 05.

Use a fresh run prefix for retries. Multiple output files are normal for parallel unloads. SINGLE and INCLUDE_QUERY_ID are different choices and cannot be combined. Avoid sensitive values in partition paths; filenames and storage metadata can expose them.

Words used in this lesson
COPY INTO @stage/path
Writes files instead of table rows.
HEADER
Include CSV column names.
SINGLE
One output file; useful for tiny exports.
INCLUDE_QUERY_ID
Unique query identifier in parallel output names.

See how it works

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

Open the interactive explanation
Interactive explainer1 / 3

Unload rows into files and verify the export

COPY INTO @stage/path
HEADER

Understand the input

COPY INTO a location exports a query result. Choose a separate output path, format and retry strategy.

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. Understand the input

    COPY INTO a location exports a query result. Choose a separate output path, format and retry strategy.

  2. Follow the example

    Export the two orders to a new path, then read the generated CSV: count 2, total 55.

  3. Check the outcome

    An export path should identify a batch. OVERWRITE does not guarantee removal of unrelated old files.

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 examples only. Start with this module’s first lesson to create L2_OBJECTS_LAB.M6_LAB. In each new SQL session select that schema and an authorized warehouse. Later lessons state additional prerequisites. Upload file contents through Snowsight or a supported client; never run CSV as SQL.

Export a tiny batch and read it back

SQL · example
COPY INTO @M6_FILES/export/run01/orders.csv
 FROM (SELECT ORDER_ID, AMOUNT FROM M6_ORDERS ORDER BY ORDER_ID)
 FILE_FORMAT=(TYPE=CSV COMPRESSION=NONE FIELD_OPTIONALLY_ENCLOSED_BY='"')
 HEADER=TRUE SINGLE=TRUE;
LIST @M6_FILES/export/run01/;
SELECT COUNT(*) AS ROW_COUNT, SUM($2::NUMBER(10,2)) AS TOTAL
 FROM @M6_FILES/export/run01/ (FILE_FORMAT => 'M6_CSV');

What to expect and why

For the original two-row input, the exported file reads back as 2 rows totaling 55. Choose run02 on a later attempt; do not mix this output with incoming files.

Check your understanding

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

COPY INTO @stage writes what?

Try it yourself

Why keep export/ separate from base/?

Compare your reasoning

It prevents an ingestion job from accidentally loading its own export and makes retention boundaries clear.

Official sources and curriculum

Module 06 · See module scope