Snowflake SQL Cheat Sheet
Snowflake SQL and warehouse management covering virtual warehouses, semi-structured data, time travel, and Snowpark basics.
Virtual Warehouse Management
Create and manage the compute layer, independent of storage.
CREATE WAREHOUSE IF NOT EXISTS etl_wh WAREHOUSE_SIZE = 'MEDIUM' AUTO_SUSPEND = 60 AUTO_RESUME = TRUE INITIALLY_SUSPENDED = TRUE;USE WAREHOUSE etl_wh;ALTER WAREHOUSE etl_wh SET WAREHOUSE_SIZE = 'LARGE';-- Multi-cluster warehouse for high concurrencyALTER WAREHOUSE etl_wh SET MIN_CLUSTER_COUNT = 1 MAX_CLUSTER_COUNT = 4 SCALING_POLICY = 'STANDARD';
Semi-Structured Data (VARIANT)
Query JSON directly without a rigid schema using VARIANT and dot/colon notation.
CREATE TABLE events (raw VARIANT);INSERT INTO eventsSELECT PARSE_JSON('{"user": {"id": 42, "tier": "gold"}, "amount": 19.99}');SELECT raw:user.id::INT AS user_id, raw:user.tier::STRING AS tier, raw:amount::FLOAT AS amountFROM events;-- Flatten nested arraysSELECT f.value:sku::STRING AS skuFROM events, LATERAL FLATTEN(input => raw:items) f;
Time Travel & Cloning
Query or restore historical data, and clone objects with zero-copy.
-- Query a table as of 1 hour agoSELECT * FROM orders AT (OFFSET => -3600);-- Query as of a specific timestampSELECT * FROM orders AT (TIMESTAMP => '2026-06-01 00:00:00'::TIMESTAMP);-- Restore an accidentally dropped tableUNDROP TABLE orders;-- Zero-copy clone (instant, no extra storage until divergence)CREATE TABLE orders_dev CLONE orders;CREATE DATABASE analytics_dev CLONE analytics_prod;
Core Concepts
Snowflake's architecture terms you'll see constantly.
- Virtual Warehouse- independently scalable compute cluster; storage and compute billed separately
- Micro-partition- Snowflake's automatic, immutable storage unit (~16MB compressed) with built-in pruning metadata
- Time Travel- query/restore historical table states within a retention window (1-90 days by edition)
- Zero-copy cloning- instantly clone a table/schema/database without duplicating storage
- Streams & Tasks- CDC-style change tracking (streams) paired with scheduled SQL execution (tasks) for pipelines
- Snowpark- DataFrame API for Python/Scala/Java that pushes compute down into Snowflake warehouses
Streams & Tasks for CDC Pipelines
Track row-level changes with a stream and act on them with a scheduled task, without external orchestration.
CREATE STREAM orders_stream ON TABLE orders;CREATE TASK refresh_summary WAREHOUSE = etl_wh SCHEDULE = '5 MINUTE'WHEN SYSTEM$STREAM_HAS_DATA('orders_stream')ASMERGE INTO order_summary tUSING orders_stream s ON t.order_id = s.order_idWHEN MATCHED THEN UPDATE SET t.amount = s.amountWHEN NOT MATCHED THEN INSERT (order_id, amount) VALUES (s.order_id, s.amount);ALTER TASK refresh_summary RESUME;
Task DAGs & Dependency Chains
Chain tasks with AFTER to build multi-step pipelines that run as a single scheduled DAG.
CREATE TASK root_task WAREHOUSE = etl_wh SCHEDULE = '1 HOUR'AS CALL load_raw();CREATE TASK child_task WAREHOUSE = etl_wh AFTER root_taskAS CALL transform_data();-- Child tasks must be resumed before the root taskALTER TASK child_task RESUME;ALTER TASK root_task RESUME;SELECT *FROM TABLE(INFORMATION_SCHEMA.TASK_HISTORY())ORDER BY scheduled_time DESC;
Clustering Keys & Query Diagnostics
Improve micro-partition pruning on large tables and inspect whether a query actually benefited from it.
ALTER TABLE events CLUSTER BY (DATE(created_at), tenant_id);SELECT SYSTEM$CLUSTERING_INFORMATION('events', '(DATE(created_at), tenant_id)');-- Speed up highly selective point lookups on a non-clustered columnALTER TABLE events ADD SEARCH OPTIMIZATION ON EQUALITY(user_id);SELECT query_id, execution_time, bytes_scanned, partitions_scanned, partitions_totalFROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY())ORDER BY start_time DESCLIMIT 5;
Snowpark DataFrame Pipeline
Push Python DataFrame transformations down into the warehouse instead of pulling data client-side.
from snowflake.snowpark import Sessionfrom snowflake.snowpark.functions import col, sum as sum_session = Session.builder.configs(connection_params).create()df = ( session.table("orders") .filter(col("status") == "completed") .group_by("customer_id") .agg(sum_("amount").alias("total_spent")))df.write.mode("overwrite").save_as_table("customer_totals")
Cost & Performance Concepts
Levers for keeping Snowflake fast and predictably billed once past ad-hoc querying.
- Result cache- an identical query on unchanged data returns instantly at zero compute cost, valid for 24 hours
- Resource Monitor- caps credit consumption per warehouse or account, can suspend warehouses or just notify at thresholds
- Micro-partition pruning- relies on automatically-maintained min/max metadata per column; clustering keys improve pruning on large, unsorted-by-load tables
- Search Optimization Service- a separately-billed feature that builds indexes to speed up highly selective equality lookups on non-clustered columns
- Query Acceleration Service- offloads scan-heavy portions of an outlier query to serverless compute so it doesn't monopolize the warehouse
- Secure Data Sharing- share live data cross-account with zero copying via shares and reader accounts
Set AUTO_SUSPEND aggressively low (60 seconds) on warehouses used for ad-hoc or bursty workloads — Snowflake bills per-second of active compute, and warehouses left idle-but-running are one of the most common sources of surprise cost.