Challenge - 5 Problems
Window Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding ROW_NUMBER() behavior
Given the following query on a sales table:
What does the ROW_NUMBER() function do in this context?
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?
Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() assigns numbers within partitions.
✗ Incorrect
ROW_NUMBER() assigns a unique sequential number to rows within each partition defined by region, ordered by sale_amount descending.
❓ service_behavior
intermediate2:00remaining
Effect of RANGE vs ROWS in window frame
Consider this query:
What is the difference in behavior if RANGE is replaced with ROWS in the window frame?
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?
Attempts:
2 left
💡 Hint
Think about how RANGE and ROWS define the window frame differently.
✗ Incorrect
RANGE uses logical offsets based on ORDER BY values (dates), so it includes all rows within 1 day before current row's date. ROWS uses physical offsets, so it includes exactly 1 preceding row regardless of date values.
❓ Configuration
advanced2: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;
Attempts:
2 left
💡 Hint
How to treat NULL values as zero in aggregation?
✗ Incorrect
COALESCE replaces NULL with 0 before summing, ensuring NULLs do not cause the cumulative sum to be NULL or incorrect.
❓ security
advanced2: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?
Attempts:
2 left
💡 Hint
Think about data visibility and access control.
✗ Incorrect
If row-level security or access controls are not correctly applied, window functions may reveal data across tenants because they operate on the full dataset before filtering.
❓ Architecture
expert2: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?
Attempts:
2 left
💡 Hint
Think about how data organization affects query speed.
✗ Incorrect
Clustering the table on partition and order columns helps Snowflake prune data and efficiently compute window functions on large datasets.