How Snowflake executes a query
A query moves through compilation, resource availability, execution operators and result delivery. Each phase can contribute to elapsed time.
Your learning goal
Create a shared orders dataset and distinguish compilation, queueing, scanning, joins, aggregation and result delivery before tuning SQL.
Understand the idea
A concrete example
The shop wants paid revenue by customer region. A slow report might scan unnecessary dates, multiply rows in a join, wait behind other queries or return too much data. Each cause needs different evidence.
| Concept | What it means |
|---|---|
| Compilation | Parse SQL, resolve objects and privileges, and choose an execution plan. |
| Operator | A unit of query work such as a scan, join or sort. |
| Queueing | Waiting for resources or provisioning before useful execution. |
| Execution | Warehouse processing of the chosen plan. |
Remember: Establish a correct result first, save the query ID and diagnose the largest observed component of elapsed time.
Explain it step by step · full theory
Learning goals and prerequisites
Create a shared orders dataset and distinguish compilation, queueing, scanning, joins, aggregation and result delivery before tuning SQL.
Use the M15_LAB setup from lesson 1. Know SELECT, joins, aggregates and window functions from Modules 03–04. Follow the supplied examples with an authorized warehouse and an isolated learning role. Keep the same data and account context when comparing runs.
Start from a business question
The shop wants paid revenue by customer region. A slow report might scan unnecessary dates, multiply rows in a join, wait behind other queries or return too much data. Each cause needs different evidence.
Define both the correct result and the acceptable delivery time. A fast incorrect regional total fails the business requirement.
Follow the work
Cloud Services coordinates parsing, metadata and optimization. A virtual warehouse supplies compute for execution. Operators can run in parallel and exchange data between workers. The visual shows logical stages, not a promise of a single serial physical pipeline.
Compilation time is different from warehouse execution time. A complex generated SQL statement may spend substantial time planning even when its result is small. Resizing a warehouse should follow evidence about the expensive phase.
Make a reproducible baseline
Keep the same input, role, warehouse size and parameter settings. Save each query ID immediately. Record cache state and concurrent load. Repeat runs because one elapsed-time observation can reflect provisioning or another workload.
The six-row fixture is for correctness and reading Query Profile. It is intentionally too small to demonstrate reliable performance gains. A later optional generated table gives you more data under a controlled size.
Common mistakes and next action
Compilation, queueing, execution and delivery have different causes.
Establish a correct result first, save the query ID and diagnose the largest observed component of elapsed time.
Words used in this lesson
- Compilation
- Parse SQL, resolve objects and privileges, and choose an execution plan.
- Operator
- A unit of query work such as a scan, join or sort.
- Queueing
- Waiting for resources or provisioning before useful execution.
- Execution
- Warehouse processing of the chosen plan.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
How Snowflake executes a query
Compile
Resolve names, authorization and available metadata before choosing operators.
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
- Compile
Resolve names, authorization and available metadata before choosing operators.
SQL → Object metadata → Plan
- Wait for resources
Provisioning and workload queues can add elapsed time before operators run.
Warehouse state → Queue → Execution slot
- Execute
Read needed data, match customer keys and aggregate paid revenue.
- Orders scan: 6 records; Filter STATUS: 5 paid rows
- Customer join: 3 unique customer keys; 5 matched paid orders
- Aggregate: east = 280; west = 370
- Deliver
A small aggregate is easy to transfer. Large results can make client retrieval significant.
Result rows → Network → Client
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.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- 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.
- 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. - 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 for L2_OBJECTS_LAB.M15_LAB. Select an authorized warehouse and a role with database/schema access and permission to create the lab objects. Setup runs once with fresh names. Optional paid services require their stated privileges and edition. SQL and performance timings have not been executed or measured for this lesson.
Create the isolated fixture
CREATE DATABASE IF NOT EXISTS L2_OBJECTS_LAB;
USE DATABASE L2_OBJECTS_LAB;
CREATE SCHEMA M15_LAB;
USE SCHEMA M15_LAB;
ALTER SESSION SET QUERY_TAG='learnsnowflake_m15';
CREATE TABLE CUSTOMERS(CUSTOMER_ID NUMBER,REGION VARCHAR);
INSERT INTO CUSTOMERS VALUES(1,'west'),(2,'east'),(3,'west');
CREATE TABLE ORDERS(ORDER_ID NUMBER,CUSTOMER_ID NUMBER,ORDER_DATE DATE,AMOUNT NUMBER(12,2),STATUS VARCHAR);
INSERT INTO ORDERS VALUES
(101,1,'2026-01-01',100,'paid'),(102,2,'2026-01-01',200,'paid'),
(103,1,'2026-01-02',150,'paid'),(104,3,'2026-01-02',50,'cancelled'),
(105,2,'2026-02-01',80,'paid'),(106,3,'2026-02-02',120,'paid');
SELECT COUNT(*) AS N,SUM(AMOUNT) AS TOTAL FROM ORDERS;What to expect and why
Expect six orders totaling 700. The January paid orders total 450. The cancelled 50 belongs to neither paid report.
| N | TOTAL |
|---|---|
| 6 | 700.00 |
Run the regional business query
SELECT c.REGION,SUM(o.AMOUNT) AS REVENUE
FROM ORDERS o JOIN CUSTOMERS c ON o.CUSTOMER_ID=c.CUSTOMER_ID
WHERE o.STATUS='paid' GROUP BY c.REGION ORDER BY c.REGION;
SET first_query_id=LAST_QUERY_ID();
SELECT $first_query_id;What to expect and why
The join maps each order to exactly one customer. Filtering removes order 104. East totals 280 and west totals 370.
| REGION | REVENUE |
|---|---|
| east | 280.00 |
| west | 370.00 |
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Predict the paid regional totals before running the query, then locate its query ID.
Compare your reasoning
Establish a correct result first, save the query ID and diagnose the largest observed component of elapsed time.
Key takeaway
Establish a correct result first, save the query ID and diagnose the largest observed component of elapsed time.
Connect this lesson to the learning path
Official sources and curriculum
Module 15 · See module scope
By Rohit Ray · Independent educational lesson · Explore this visual lab