Window functions in Snowflake - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using window functions in Snowflake, it's important to know how the work grows as your data grows.
We want to understand how the number of operations changes when the input data size increases.
Analyze the time complexity of the following operation sequence.
SELECT
user_id,
order_date,
amount,
SUM(amount) OVER (PARTITION BY user_id ORDER BY order_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM orders;
This query calculates a running total of order amounts for each user, ordered by date.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Calculating the running total for each row using the window function.
- How many times: Once per row in the input data.
As the number of rows grows, the work to compute running totals grows roughly in proportion to the number of rows.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 running total calculations |
| 100 | About 100 running total calculations |
| 1000 | About 1000 running total calculations |
Pattern observation: The number of operations grows linearly with the number of rows.
Time Complexity: O(n)
This means the time to compute the running totals grows directly with the number of rows.
[X] Wrong: "Window functions always take much longer than simple queries because they do a lot more work."
[OK] Correct: While window functions do extra calculations, Snowflake optimizes them so the work grows linearly, not exponentially, with data size.
Understanding how window functions scale helps you explain query performance clearly and shows you know how databases handle data efficiently.
"What if we changed the window frame from UNBOUNDED PRECEDING to a fixed number of rows? How would the time complexity change?"
Practice
Solution
Step 1: Understand window function purpose
Window functions perform calculations across a set of rows related to the current row but do not reduce the number of rows returned.Step 2: Compare with grouping
Unlike GROUP BY, window functions keep all rows visible while calculating values like running totals or ranks.Final Answer:
Calculates values across rows related to the current row without grouping them into fewer rows -> Option AQuick Check:
Window functions analyze rows without grouping = A [OK]
- Confusing window functions with GROUP BY aggregation
- Thinking window functions reduce row count
- Assuming window functions delete duplicates
Solution
Step 1: Identify correct window function syntax
SUM(sales) OVER (PARTITION BY region ORDER BY date) correctly calculates a running total partitioned by region and ordered by date.Step 2: Eliminate incorrect options
SELECT SUM(sales) GROUP BY region ORDER BY date FROM sales_data; uses GROUP BY which reduces rows, not a window function. Options C and D use invalid functions or syntax.Final Answer:
SELECT SUM(sales) OVER (PARTITION BY region ORDER BY date) FROM sales_data; -> Option DQuick Check:
SUM() OVER with PARTITION BY and ORDER BY = B [OK]
- Using GROUP BY instead of OVER clause
- Using non-existent functions like RUNNING_TOTAL
- Omitting ORDER BY in window function
sales with columns region, date, and amount, what is the output of this query?SELECT region, date, amount, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank FROM sales;
Solution
Step 1: Understand RANK() with PARTITION BY and ORDER BY
RANK() assigns ranks starting at 1 within each partition (region), ordering by amount descending.Step 2: Interpret the query output
The query shows each sale with its rank in its region based on amount, highest amount ranked 1.Final Answer:
Ranks sales amounts within each region from highest to lowest -> Option AQuick Check:
RANK() OVER PARTITION BY region ORDER BY amount DESC = A [OK]
- Thinking RANK() ignores PARTITION BY
- Confusing RANK() with cumulative sum
- Assuming ranks are across all rows without grouping
SELECT employee_id, salary, ROW_NUMBER() OVER (ORDER BY salary) PARTITION BY department FROM employees;
Solution
Step 1: Check window function clause order
In Snowflake, PARTITION BY must appear before ORDER BY inside the OVER() clause.Step 2: Identify syntax error
The query places PARTITION BY after ORDER BY, which is invalid syntax.Final Answer:
PARTITION BY must come before ORDER BY inside OVER() -> Option CQuick Check:
PARTITION BY before ORDER BY in OVER() = D [OK]
- Placing PARTITION BY after ORDER BY
- Thinking ROW_NUMBER() disallows ORDER BY
- Adding unnecessary GROUP BY for window functions
Solution
Step 1: Use AVG() as window function partitioned by region
AVG(amount) OVER (PARTITION BY region) calculates average sales per region without grouping rows.Step 2: Use RANK() partitioned by region ordered by amount descending
RANK() OVER (PARTITION BY region ORDER BY amount DESC) ranks sales within each region.Step 3: Verify query correctness
SELECT region, amount, AVG(amount) OVER (PARTITION BY region) AS avg_region, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank FROM sales; correctly uses window functions with proper syntax and clauses.Final Answer:
SELECT region, amount, AVG(amount) OVER (PARTITION BY region) AS avg_region, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank FROM sales; -> Option BQuick Check:
AVG() and RANK() with PARTITION BY region = C [OK]
- Using GROUP BY instead of window functions
- Incorrect syntax for window functions
- Omitting PARTITION BY for per-region calculations
