0
0
Snowflakecloud~20 mins

Window functions in Snowflake - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding ROW_NUMBER() behavior
Given the following query on a sales table:
SELECT salesperson_id, sale_amount, ROW_NUMBER() OVER (PARTITION BY region ORDER BY sale_amount DESC) AS rank FROM sales;

What does the ROW_NUMBER() function do in this context?
ACounts the number of sales per salesperson across all regions.
BCalculates the total sales amount per region.
CReturns the cumulative sum of sale_amount ordered by region.
DAssigns a unique rank to each sale within each region, ordered by sale_amount descending.
Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() assigns numbers within partitions.
service_behavior
intermediate
2:00remaining
Effect of RANGE vs ROWS in window frame
Consider this query:
SELECT order_id, order_date, SUM(amount) OVER (ORDER BY order_date RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND CURRENT ROW) AS sum_amount FROM orders;

What is the difference in behavior if RANGE is replaced with ROWS in the window frame?
ARANGE includes all rows with order_date within 1 day before current row's date; ROWS includes exactly 1 preceding row regardless of date.
BROWS includes all rows with order_date within 1 day before current row's date; RANGE includes exactly 1 preceding row.
CRANGE and ROWS behave identically in this query.
DRANGE causes a syntax error in Snowflake.
Attempts:
2 left
💡 Hint
Think about how RANGE and ROWS define the window frame differently.
Configuration
advanced
2:00remaining
Correct use of window function with NULL handling
You want to calculate the cumulative sum of sales amounts ordered by sale_date, but some sale_amount values are NULL. Which query correctly treats NULL as zero in the cumulative sum?
SELECT sale_date, sale_amount, SUM(sale_amount) OVER (ORDER BY sale_date ROWS UNBOUNDED PRECEDING) AS cum_sum FROM sales;
ASELECT sale_date, sale_amount, SUM(sale_amount) OVER (ORDER BY sale_date RANGE UNBOUNDED PRECEDING) AS cum_sum FROM sales;
BSELECT sale_date, sale_amount, SUM(sale_amount) OVER (ORDER BY sale_date ROWS UNBOUNDED PRECEDING) AS cum_sum FROM sales WHERE sale_amount IS NOT NULL;
CSELECT sale_date, sale_amount, SUM(COALESCE(sale_amount, 0)) OVER (ORDER BY sale_date ROWS UNBOUNDED PRECEDING) AS cum_sum FROM sales;
DSELECT sale_date, sale_amount, SUM(NULLIF(sale_amount, 0)) OVER (ORDER BY sale_date ROWS UNBOUNDED PRECEDING) AS cum_sum FROM sales;
Attempts:
2 left
💡 Hint
How to treat NULL values as zero in aggregation?
security
advanced
2:00remaining
Security implications of window functions in shared environments
In a multi-tenant Snowflake environment, what is a key security consideration when using window functions on shared tables?
AWindow functions automatically encrypt data during processing.
BWindow functions can expose data from other tenants if row-level security is not properly enforced.
CWindow functions prevent SQL injection attacks by design.
DWindow functions restrict access to only the first row of each partition.
Attempts:
2 left
💡 Hint
Think about data visibility and access control.
Architecture
expert
2:00remaining
Optimizing performance of window functions on large datasets
You have a large table with millions of rows and want to optimize a query using window functions with PARTITION BY and ORDER BY clauses. Which approach will most improve query performance in Snowflake?
ACluster the table on the columns used in PARTITION BY and ORDER BY to reduce data scanned during window function execution.
BUse CROSS JOIN to pre-aggregate data before applying window functions.
CAvoid using PARTITION BY and ORDER BY in window functions to speed up queries.
DDisable automatic clustering to let Snowflake handle data distribution.
Attempts:
2 left
💡 Hint
Think about how data organization affects query speed.