0
0
SQLquery~20 mins

Percent of total with window functions in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Percent of Total Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate percent of total sales per product
Given the sales table with columns product_id and amount, which query correctly calculates the percent of total sales for each product?
SQL
SELECT product_id, amount, 
       100.0 * amount / SUM(amount) OVER () AS percent_of_total
FROM sales;
ASELECT product_id, amount, amount / SUM(amount) OVER (PARTITION BY product_id) AS percent_of_total FROM sales;
BSELECT product_id, amount, 100.0 * SUM(amount) OVER () / amount AS percent_of_total FROM sales;
CSELECT product_id, amount, 100.0 * amount / SUM(amount) OVER () AS percent_of_total FROM sales;
DSELECT product_id, amount, 100.0 * amount / SUM(amount) FROM sales GROUP BY product_id;
Attempts:
2 left
💡 Hint
Use a window function without partitioning to get the total sum over all rows.
query_result
intermediate
2:00remaining
Percent of total with partition by category
Which query calculates the percent of total sales within each category using window functions? The table sales has columns category and amount.
SQL
SELECT category, amount, 
       100.0 * amount / SUM(amount) OVER (PARTITION BY category) AS percent_of_category_total
FROM sales;
ASELECT category, amount, 100.0 * amount / SUM(amount) OVER (PARTITION BY category) AS percent_of_category_total FROM sales;
BSELECT category, amount, 100.0 * SUM(amount) OVER () / amount AS percent_of_category_total FROM sales;
CSELECT category, amount, amount / SUM(amount) OVER () AS percent_of_category_total FROM sales;
DSELECT category, amount, 100.0 * amount / SUM(amount) FROM sales GROUP BY category;
Attempts:
2 left
💡 Hint
Partition the sum by category to get totals per category.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in percent calculation query
Which option contains a syntax error when calculating percent of total sales using window functions?
SQL
SELECT product_id, amount, 100 * amount / SUM(amount) OVER () AS percent_of_total FROM sales;
ASELECT product_id, amount, 100 * amount / SUM(amount) OVER () AS percent_of_total FROM sales;
BSELECT product_id, amount, 100 * amount / SUM(amount) OVER PARTITION BY () AS percent_of_total FROM sales;
CSELECT product_id, amount, 100.0 * amount / SUM(amount) OVER () AS percent_of_total FROM sales;
DSELECT product_id, amount, 100 * amount / SUM(amount) OVER (PARTITION BY product_id) AS percent_of_total FROM sales;
Attempts:
2 left
💡 Hint
Check the syntax of the OVER clause and partitioning.
optimization
advanced
2:00remaining
Optimize percent of total calculation for large datasets
Which query is the most efficient way to calculate percent of total sales per product when the sales table is very large?
AWITH total AS (SELECT SUM(amount) AS total_amount FROM sales) SELECT product_id, amount, 100.0 * amount / total_amount AS percent_of_total FROM sales, total;
BSELECT product_id, amount, 100.0 * amount / (SELECT SUM(amount) FROM sales) AS percent_of_total FROM sales;
CSELECT product_id, amount, 100.0 * amount / SUM(amount) OVER () AS percent_of_total FROM sales;
DSELECT product_id, amount, 100.0 * amount / SUM(amount) FROM sales GROUP BY product_id;
Attempts:
2 left
💡 Hint
Calculate the total sum once, then join to avoid repeated aggregation.
🧠 Conceptual
expert
3:00remaining
Understanding window function behavior in percent calculations
Consider the query:
SELECT product_id, amount, 100.0 * amount / SUM(amount) OVER (PARTITION BY category ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_percent FROM sales;

What does the running_percent column represent?
AThe percent of the current amount relative to the total sales of all products on the current date only.
BThe percent of the current amount relative to the total sales of all categories regardless of date.
CThe percent of the current amount relative to the total sales in the category, ignoring date order.
DThe percent of the current amount relative to the total sales in the same category up to the current date (running total percent).
Attempts:
2 left
💡 Hint
Look at the window frame defined by ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.