0
0
Snowflakecloud~10 mins

Window functions in Snowflake - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Window functions in Snowflake
Start Query
Select Data
Define Window
Apply Window Function
Calculate Result per Row
Return Result Set
The query starts by selecting data, then defines a window (a group of rows), applies the window function to each row within that window, calculates results, and returns the final result set.
Execution Sample
Snowflake
SELECT
  employee_id,
  department,
  salary,
  AVG(salary) OVER (PARTITION BY department ORDER BY employee_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS avg_salary
FROM employees;
This query calculates the running average salary per department ordered by employee_id.
Process Table
StepRow (employee_id)Partition (department)Window FrameWindow Function Calculationavg_salary
1101SalesRows from start to current (101)AVG(salary) of [5000]5000
2102SalesRows from start to current (101,102)AVG(salary) of [5000, 6000]5500
3103SalesRows from start to current (101,102,103)AVG(salary) of [5000, 6000, 6500]5833.33
4201HRRows from start to current (201)AVG(salary) of [4500]4500
5202HRRows from start to current (201,202)AVG(salary) of [4500, 4700]4600
6301ITRows from start to current (301)AVG(salary) of [7000]7000
7302ITRows from start to current (301,302)AVG(salary) of [7000, 7200]7100
8303ITRows from start to current (301,302,303)AVG(salary) of [7000, 7200, 7100]7100
9END---All rows processed, query returns result set
💡 All rows processed, window function applied per partition and ordered rows, query completes.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
employee_id-101102103201202301302303-
department-SalesSalesSalesHRHRITITIT-
salary-50006000650045004700700072007100-
avg_salary-500055005833.3345004600700071007100-
Key Moments - 3 Insights
Why does the average salary change only within each department and not across all employees?
Because the window function uses PARTITION BY department, it calculates averages separately for each department partition as shown in execution_table rows 1-3 for Sales, 4-5 for HR, and 6-8 for IT.
What does the window frame 'ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW' mean?
It means the function calculates the average from the first row in the partition up to the current row, accumulating values as seen in the execution_table where the window frame grows with each row.
Why is the order by employee_id important in the window function?
Ordering by employee_id defines the sequence in which rows are considered for the running average, affecting the calculation step by step as shown in the execution_table where avg_salary changes with each ordered row.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the avg_salary value for employee_id 202 in HR?
A4500
B4700
C4600
D4550
💡 Hint
Check the avg_salary column in execution_table row 5 for employee_id 202.
At which step does the window function finish processing all rows in the IT department?
AStep 8
BStep 7
CStep 6
DStep 9
💡 Hint
Look at the partition column and see when IT department rows end in execution_table.
If we remove ORDER BY employee_id from the window clause, how would the avg_salary values change?
AThey would be calculated over the entire partition without order, so running average would not accumulate.
BThe avg_salary would be the overall average per department for all rows.
CThe query would fail with an error.
DThey would be the same as now.
💡 Hint
Consider how ORDER BY affects the window frame and calculation in execution_table steps.
Concept Snapshot
Window functions in Snowflake:
- Use OVER() clause with PARTITION BY and ORDER BY
- PARTITION BY groups rows like departments
- ORDER BY defines row sequence inside partitions
- Window frame defines rows considered per calculation
- Functions compute values per row over the window
- Useful for running totals, averages, ranks without grouping
Full Transcript
This visual execution traces a Snowflake window function calculating running average salary per department. The query selects employee data and applies AVG() over a window partitioned by department and ordered by employee_id. Each step shows how the window frame grows from the first row to the current row in the partition, updating the average salary. Variables track employee_id, department, salary, and the computed avg_salary. Key moments clarify partitioning, window frame meaning, and ordering importance. The quiz tests understanding of values at specific steps and effects of removing ORDER BY. The snapshot summarizes syntax and behavior for quick reference.