← Notes

Snowflake Notes: from your first SQL query to an orders pipeline

Contents

An order feed sends nine rows. One order appears twice, an older version arrives late, a customer gets a refund, and one amount reads oops. What would SUM(amount) give you? This note starts with that small problem and builds a Snowflake pipeline whose daily totals you can check by hand.

Where to start. If you know a little SELECT, you can follow the examples. Spend about 20 minutes on the mental model and first query, then allow 45–60 minutes for the lab. It uses synthetic orders. Download the SQL lab · Download the nine orders as comma-separated values (CSV).

1. The mental model

Snowflake is a managed cloud data platform. Start by thinking of it as an analytical database. An application database might record a payment and update the order in one transaction. Snowflake can combine historical records from several systems and answer which sales channel led last month. These are typical examples of online transaction processing (OLTP) and online analytical processing (OLAP), respectively. This guide uses standard tables; Snowflake also offers Hybrid Tables for low-latency transactional access.

Figure 1. Two virtual warehouses can query one table. Suspending the reporting warehouse leaves the stored orders intact.
LayerJobWhere you see it in the lab
Cloud servicesHandles authentication, permissions, metadata, and query planningYour active role determines whether the query is allowed
ComputeVirtual warehouses execute queries, data loads, and transformationsSF_LEARN_ORDERS_WH provides the compute
StoragePersists standard tables in compressed columnar form and manages their micro-partitionsSF_LEARN_ORDERS.CORE.ORDERS remains available after the warehouse is suspended and resumed

A database is a namespace for objects. A virtual warehouse is compute. Point a query at the same table from a different warehouse and you change the resources and which warehouse accrues usage. Giving reports and batch transformations separate warehouses can isolate their compute workloads. Snowflake architecture and core objects

ObjectHow to recognize it
AccountBoundary for sign-in, account-level roles, and usage
Database → schema → table or viewA fully qualified name looks like SF_LEARN_ORDERS.MART.DAILY_PAID_SALES. A regular view stores a query definition
RoleA bundle of privileges. The same SQL can work under one role and fail under another
Stage / file formatA stage tells Snowflake where files are; a file format tells it how to read CSV, JavaScript Object Notation (JSON), or Parquet

Snowflake normally resolves unquoted identifiers in uppercase. A double-quoted identifier preserves its case and usually needs an exact match. The lab uses unquoted names and single quotes for string literals.

2. Set up and run your first query

Use a personal trial or an approved sandbox. The trial documentation says a trial ends after 30 days or when its usage balance is exhausted, whichever comes first. Check the remaining balance before starting. In Snowsight, open Projects → Workspaces → My Workspace, create a SQL file, and check the role and warehouse in the editor. Use Workspaces when older tutorials refer to Legacy Worksheets.

Make a small sandbox

With the SYSADMIN role in your personal sandbox, create the objects below. In a shared account, ask for an approved lab database, warehouse, and role, then substitute those names. Run one section at a time in the same SQL session. If either CREATE finds a pre-existing object, stop and verify that you own it or choose fresh names.

USE ROLE SYSADMIN;

CREATE WAREHOUSE sf_learn_orders_wh
  WAREHOUSE_TYPE = 'STANDARD'
  WAREHOUSE_SIZE = 'XSMALL'
  AUTO_SUSPEND = 60
  AUTO_RESUME = TRUE
  INITIALLY_SUSPENDED = TRUE
  STATEMENT_TIMEOUT_IN_SECONDS = 120;

CREATE DATABASE sf_learn_orders DATA_RETENTION_TIME_IN_DAYS = 1;
USE WAREHOUSE sf_learn_orders_wh;
USE DATABASE sf_learn_orders;
CREATE SCHEMA raw;
CREATE SCHEMA core;
CREATE SCHEMA mart;

SELECT CURRENT_ROLE(), CURRENT_WAREHOUSE(), CURRENT_DATABASE();

The warehouse starts suspended. It can resume when a statement needs compute and suspend after it has been idle for about a minute. The statement timeout limits the time for an individual statement; it is not a budget. If an object is missing or access is denied, check the active role, database, and schema. Try the fully qualified name, then check privileges on the warehouse, the parent objects, and the table or view.

Query an existing table

If your account has installed SNOWFLAKE_SAMPLE_DATA and you have access, count how often each order priority appears:

SELECT o_orderpriority AS priority, COUNT(*) AS orders
FROM snowflake_sample_data.tpch_sf1.orders
GROUP BY o_orderpriority
ORDER BY orders DESC, priority
LIMIT 5;

TPCH_SF1 is a scale-factor-one sample for the TPC-H benchmark from the Transaction Processing Performance Council. Start with the smaller sample. The engine still has to aggregate before LIMIT 5 restricts the returned rows. You can skip this query if the sample database is unavailable; the orders lab is self-contained. Sample data and access

3. Lab: from raw orders to daily paid sales

Define the grain and metric before writing SQL. The core table has one row per order_id, representing the latest valid version. The daily view counts orders that are currently PAID and sums their amounts in US dollars, grouped by original order date. A later refund removes the order from the earlier day’s metric. This is a current-state report by order date. A finance ledger for daily cash movement would use a separate fact table at the payment or refund transaction grain and group by transaction time.

Figure 2. RAW keeps the received values. CORE has one row per order. MART has one row per date with paid sales.

Step A: load the raw rows

Use SQL for the first run. Open the downloaded script, run Section 1, and use the CREATE TABLE and INSERT in Section 2. The raw date, amount, and version columns keep the original strings, including invalid ones.

To practice a CSV load instead of the INSERT, create the same empty raw table, then run the first two statements below. In Snowsight choose Ingestion → Add Data → Load files into a Stage and upload orders.csv to the root of the named internal stage SF_LEARN_ORDERS.RAW.ORDERS_INBOX. The web SQL editor cannot read a local path through PUT; use the stage upload screen or run PUT through the Snowflake command-line interface (CLI), for example with snow sql. Stage a file in Snowsight · PUT reference

CREATE STAGE raw.orders_inbox;
CREATE FILE FORMAT raw.orders_csv
  TYPE = CSV
  SKIP_HEADER = 1
  FIELD_OPTIONALLY_ENCLOSED_BY = '"';

-- Upload orders.csv to the named stage before running the following.
LIST @raw.orders_inbox;

COPY INTO raw.order_events
  FROM @raw.orders_inbox
  FILES = ('orders.csv')
  FILE_FORMAT = (FORMAT_NAME = 'SF_LEARN_ORDERS.RAW.ORDERS_CSV')
  VALIDATION_MODE = RETURN_ALL_ERRORS;

COPY INTO raw.order_events
  FROM @raw.orders_inbox
  FILES = ('orders.csv')
  FILE_FORMAT = (FORMAT_NAME = 'SF_LEARN_ORDERS.RAW.ORDERS_CSV')
  ON_ERROR = 'ABORT_STATEMENT';

SELECT COUNT(*) AS raw_rows FROM raw.order_events;

Expect no validation errors and nine rows loaded. File validation accepts oops in a text column; business validation comes next. Snowflake uses load metadata to avoid reloading known files into the same table. A duplicate order can still arrive in another file, and FORCE = TRUE can reload known files. Keep file-level load tracking separate from order-level deduplication. COPY INTO table

Step B: validate, deduplicate, then merge

Run the rest of Section 2. The raw.order_events_checked view uses TRY_TO_DATE and TRY_TO_DECIMAL so bad conversions yield NULL. Format checks prevent silently rounding amounts with more than two decimal places or parsing fractional version numbers. The lab accepts non-negative amounts and positive integer versions. Expect a single rejection: row 8, order O1005, reason BAD_AMOUNT. In a real pipeline, record rejection rates and keep a way to repair and replay rows.

OrderWhat arrived in RAWWhat belongs in CORE
O1001Version 2, 120.00, arrived before version 1, 100.00Version 2. Arrival order does not establish source order
O1003Paid, then refundedVersion 2, REFUNDED. Keep the order out of paid sales
O1004The same version and payload arrived twiceOne order row

Here is Section 3’s key statement. The source subquery picks the highest version per order. A unique ingest_id breaks ties for identical replays. The match condition then prevents an older version from replacing a newer target row.

SET batch_first = 1;
SET batch_last = 9;

MERGE INTO core.orders AS t
USING (
  SELECT order_id, order_date, status, amount, source_version
  FROM raw.order_events_checked
  WHERE reject_reason IS NULL AND ingest_id BETWEEN $batch_first AND $batch_last
  QUALIFY ROW_NUMBER() OVER (
    PARTITION BY order_id ORDER BY source_version DESC, ingest_id DESC
  ) = 1
) AS s ON t.order_id = s.order_id
WHEN MATCHED AND s.source_version > t.source_version THEN UPDATE SET
  t.order_date = s.order_date,
  t.status = s.status,
  t.amount = s.amount,
  t.source_version = s.source_version
WHEN NOT MATCHED THEN INSERT (order_id, order_date, status, amount, source_version)
  VALUES (s.order_id, s.order_date, s.status, s.amount, s.source_version);

WHERE filters rows before the window function assigns ranks; QUALIFY filters after it. MERGE does not choose the winner for you. Multiple source rows matching one target can cause an update error, and duplicate unmatched rows can all be inserted. On standard tables, primary, unique, and foreign keys are not enforced. Test the grain explicitly. If the editor reconnects, set the batch variables again before running the MERGE. QUALIFY · MERGE · Constraints

The lab assumes one writer and identical payloads for the same key and source version. Check for conflicts across batches in production. Since invalid rows are filtered before ranking, an invalid new version can leave the last valid state visible while a correction is pending. Make that outcome visible to operators and decide whether the business should continue serving the older value.

Step C: check the numbers, then replay a batch

The mart.daily_paid_sales view reads paid orders from core.orders and groups COUNT(*) and SUM(amount) by order_date.

SELECT COUNT(*) AS order_rows FROM core.orders;

SELECT order_id, COUNT(*) AS n
FROM core.orders
GROUP BY order_id
HAVING COUNT(*) > 1;

SELECT * FROM mart.daily_paid_sales ORDER BY order_date;
Order dateBatch A: count / amount (USD)After batch B: count / amount (USD)
2026-11-012 / 195.502 / 190.50
2026-11-021 / 80.002 / 105.00

After batch A, expect five core rows and no duplicate keys. November 1 adds 120.00 and 75.50; November 2 counts O1004 once. Now run Section 4: insert batch B once, then run the MERGE twice. The first run updates O1002 to 70.50 and inserts O1007 at 25.00, while ignoring the old version of O1001. The second run should report zero updates and inserts. This demonstrates idempotence in the transformation. Repeating the raw INSERT would still add rows. In production, track stable event identifiers and advance a batch checkpoint only after successful processing.

4. SQL you will use again

JSON: expand one order into its line items

VARIANT stores semi-structured values. PARSE_JSON turns a string into a structured value; use a colon to reach a key and cast it to the type you need. LATERAL FLATTEN can produce several rows for every input order.

WITH orders AS (
  SELECT PARSE_JSON('{
    "order_id": "O1001",
    "items": [{"sku":"A","qty":2,"price":40.00}, {"sku":"B","qty":1,"price":40.00}]
  }') AS payload
)
SELECT o.payload:order_id::VARCHAR AS order_id,
       f.value:sku::VARCHAR AS sku,
       f.value:qty::NUMBER(10, 0) * f.value:price::NUMBER(12, 2) AS line_amount
FROM orders AS o, LATERAL FLATTEN(INPUT => o.payload:items) AS f
ORDER BY sku;

Expect 80.00 for A and 40.00 for B. Summing a repeated order total over these two rows would yield 240.00 instead of 120.00. Sum line amounts or regain order grain before joining to another fact. Empty arrays produce no rows by default; see OUTER => TRUE if you need to preserve the left row. Decide separately what empty arrays, missing keys, and JSON null mean for your metric. Semi-structured queries · FLATTEN

Dates: decide where midnight falls

The lab receives an order date, so DATE fits. With timestamped events, save the original event time and record when the data became available. Define the reporting date in the business time zone. For example, if a timestamp without a time zone (TIMESTAMP_NTZ) represents Coordinated Universal Time (UTC), this query returns 2026-11-01:

SELECT CONVERT_TIMEZONE(
  'UTC', 'America/Chicago', '2026-11-02 02:00:00'::TIMESTAMP_NTZ
)::DATE AS reporting_date;

A named time zone such as America/Chicago accounts for changes to the local offset. For historical model features, the difference between occurrence time and availability time matters as well. CONVERT_TIMEZONE

5. Query performance and costs

Figure 3. A filter for November 2 can skip three of these illustrative micro-partitions. Scanning the matching partition still requires filtering its rows.

Snowflake automatically divides standard tables into micro-partitions and records metadata such as column ranges. With a predicate like WHERE order_date >= '2026-11-02'::DATE AND order_date < '2026-11-03'::DATE, Snowflake can skip partitions whose dates cannot match. This is pruning. If every partition has some records for November 2, the filter might eliminate little at the partition level. The nine lab rows teach grain and correctness; choose a larger sample to study pruning. Micro-partitions and clustering

Find the slow part in Query Profile

In Snowsight, find the statement in Query History and open Query Profile. Record the query ID and warehouse. Separate queue time, compilation, and execution, then inspect the expensive scans, joins, or aggregations.

SymptomFirst check
Long queue, short executionCheck whether reports share compute with batch jobs. Split workloads or evaluate a multi-cluster warehouse for concurrency; check edition requirements
Most partitions scanned for a narrow questionCheck the filter and projected columns. Compare partitions scanned, total partitions, and bytes scanned. Consider clustering and its maintenance cost if a large table sees this continually
A join emits far more rows than expectedCheck the grain of both sides and look for missing join conditions or non-unique keys
Sort or aggregate spills heavily to remote storageReduce the intermediate data first. Then compare a larger warehouse if the query still spills
An identical rerun suddenly gets much fasterSeparate persisted result reuse and warehouse data caching. In a benchmark session, ALTER SESSION SET USE_CACHED_RESULT = FALSE; bypasses result reuse. Record the warehouse’s cache state too

Query History and Profile · Warehouse considerations · Persisted query results

Measure the difference. If you can access the sample data, run the January 1995 query below. Then run it without the WHERE clause. Compare partitions and bytes in the respective TableScan operators. These queries answer different questions; the experiment illustrates scan scope. When benchmarking a rewrite, verify that the outputs agree first.

ALTER SESSION SET USE_CACHED_RESULT = FALSE;

SELECT SUM(o_totalprice) AS total_price
FROM snowflake_sample_data.tpch_sf1.orders
WHERE o_orderdate >= DATE '1995-01-01'
  AND o_orderdate < DATE '1995-02-01';

-- After measuring both queries, restore the default behavior.
ALTER SESSION UNSET USE_CACHED_RESULT;

Check credits alongside latency

An active virtual warehouse is billed by the second, with a one-minute minimum each time it starts or resumes. At an illustrative rate of one credit per hour, 12 continuous minutes cost about 0.20 credits. Twelve 10-second runs that each start a suspended warehouse also cost at least 0.20 credits. The example excludes other services and does not quote a price. Storage and serverless features can incur costs independently of the lab warehouse. Compute cost

Compare several runs under the same role, data range, and cache conditions. Check usage as well as latency. A resource monitor applies to user-managed warehouses; check separate usage and budgets for serverless features such as Snowpipe. Resource monitors

6. Beyond the lab: pipelines, access, and recovery

Match the tool to the input and the failure mode

NeedStart withCheck before adopting
Scheduled CSV or Parquet batchesStage + COPY INTOBatch identity, reject handling, and load history
Files arriving throughout the daySnowpipeFile sizes, freshness, duplicates, and serverless usage
A producer sends rows directlySnowpipe StreamingChannels, source offsets, and replay policy
A frequently queried derived table defined by a SELECTDynamic TableTARGET_LAG expresses a freshness target. Check the supported SQL, refresh mode, actual lag, and cost
Custom processing of changes, such as upserts and deletesStream + TaskA stream tracks a change offset. Reading it with SELECT does not advance it; committed consumption through a write does. Separate consumers usually need separate streams
A team maintains models and shared metricsdbt with Snowflakedbt organizes models and tests; Snowflake runs the SQL. Add unique order-key, not-null, and accepted-status checks

Data loading options · Dynamic Tables decision guide · Introduction to streams · dbt’s Snowflake setup

Give readers access to the view

Role-based access control (RBAC) lets you keep report consumption separate from pipeline writes. An analyst needs warehouse USAGE, database and schema USAGE, and SELECT on the report view. Grant those permissions through an authorized role. Leave raw-table access and writes to the pipeline role. Test with the actual reporting role: the report should open and a direct read of RAW should fail. Access control

Inspect history before restoring data

Time Travel can query historical data within its retention window. A zero-copy clone initially shares the underlying storage; changes can add storage over time. In a sandbox, replace the placeholder with the query ID of a change you want to inspect:

SELECT order_id, status, amount
FROM sf_learn_orders.core.orders
BEFORE (STATEMENT => '<query_id_of_the_change>')
ORDER BY order_id;

BEFORE returns the state preceding the specified change. Retention depends on configuration, object type, and edition. Long-term audit or recovery outside that window needs its own plan. Time Travel · AT / BEFORE

7. Apply it to reporting, data science, or fraud

ScenarioA useful first projectFirst validation
Business intelligence (BI)A read-only dashboard on MART.DAILY_PAID_SALESPick a day, trace its amount to source orders, and see where a refund changes the report
Data scienceCompute historical aggregates with Snowpark Python and pull back a small resultA feature may use only records available at the label cutoff. A current order snapshot can leak a future refund
Real-time fraudPrepare historical features, evaluate models, and inspect drift in the warehouseEvaluate an online feature service or low-latency store if the request has a tight tail-latency target

To run this locally, install snowflake-snowpark-python in a virtual environment, then configure a named connection called orders_lab with an approved warehouse and a role that can read MART. Follow the authentication instructions in the session guide. The example needs no credentials in the source. Set up Snowpark Python

from snowflake.snowpark import Session

session = Session.builder.config("connection_name", "orders_lab").create()
try:
    rows = (
        session.table("SF_LEARN_ORDERS.MART.DAILY_PAID_SALES")
        .sort("ORDER_DATE")
        .limit(10)
        .collect()
    )
    for row in rows:
        print(row)
finally:
    session.close()

Snowpark DataFrame transformations are generally lazy until an action such as collect(). Limit and aggregate in Snowflake first. collect() brings rows back to Python, so this is suitable for a small daily report. Create a Snowpark session · Snowpark DataFrames

8. Try these next, then clean up

  1. Send O1007 as a newer refund and an older paid record. Predict which day’s sales change, then replay the batch and check the amount.
  2. Give O1001 two line items. Sum its order total after joining and explain the result. Rewrite the query to sum at order grain.
  3. Run two equivalent scans over a large sample with different filter strategies, or compare with and without a date range. Record bytes, partitions, and runtime. Explain whether extra compute addresses what you saw.
  4. Hand someone the metric definition, the batch boundaries, where to find rejects, and the steps for a read-only role. Have them reconcile one day independently.

Suspend the lab warehouse when you finish, or skip the first statement if it has already suspended. If the database and warehouse are disposable, check their names and drop them one statement at a time. The downloadable lab does not execute these operations.

ALTER WAREHOUSE sf_learn_orders_wh SUSPEND;
-- Only after checking these are your lab's disposable objects:
DROP DATABASE sf_learn_orders;
DROP WAREHOUSE sf_learn_orders_wh;

9. A reading path

OrderResourceWhat to do with it
1Snowflake in 20 minutesRun the load and query yourself. Use the Workspaces guide if the interface looks different
2Snowflake University: Data Warehousing WorkshopLoad CSV and JSON yourself and check what reaches the table
3Official hands-on tutorialsPick one cloud source such as Amazon S3 or Azure and practice a staged load
4Query History and Query ProfileUse operator timings and scan metrics to explain a slow query and verify a change
5Snowflake University: Hands-On EssentialsAfter Badge 1, pick Data Engineering, Data Science, or Data Applications to fit your goal
6The Snowflake Elastic Data Warehouse, SIGMOD 2016 (paper)Read why the authors chose shared storage and multi-cluster compute. Use current documentation for today’s product behavior