Challenge - 5 Problems
Percent of Total Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Use a window function without partitioning to get the total sum over all rows.
✗ Incorrect
Option C correctly divides each amount by the total sum of all amounts using a window function without partitioning, giving the percent of total sales per product.
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Partition the sum by category to get totals per category.
✗ Incorrect
Option A uses a window function partitioned by category to calculate the percent of total sales within each category.
📝 Syntax
advanced2: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;
Attempts:
2 left
💡 Hint
Check the syntax of the OVER clause and partitioning.
✗ Incorrect
Option B has invalid syntax: 'OVER PARTITION BY ()' is incorrect. The correct syntax is 'OVER ()' or 'OVER (PARTITION BY column)'.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Calculate the total sum once, then join to avoid repeated aggregation.
✗ Incorrect
Option A calculates the total sum once in a CTE and joins it, avoiding repeated aggregation for each row, which is more efficient on large datasets.
🧠 Conceptual
expert3:00remaining
Understanding window function behavior in percent calculations
Consider the query:
What does the
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?Attempts:
2 left
💡 Hint
Look at the window frame defined by ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
✗ Incorrect
The window frame defines a running total within each category ordered by date, so the percent is relative to the cumulative sum up to the current row's date.