Calculate Percent of Total Sales Using Window Functions
📖 Scenario: You work in a retail company database. You have a table of sales data showing how much each product sold. You want to find out what percentage of the total sales each product represents.
🎯 Goal: Create a SQL query that calculates the percent of total sales for each product using window functions.
📋 What You'll Learn
Create a table called sales with columns product (text) and amount (integer).
Insert the exact sales data for 5 products: 'Apples' 100, 'Bananas' 150, 'Cherries' 50, 'Dates' 200, 'Elderberries' 100.
Write a SQL query that selects product, amount, and a new column percent_of_total which shows the sales amount as a percentage of the total sales.
Use a window function with SUM(amount) OVER () to calculate the total sales.
Format the percent_of_total as a decimal number between 0 and 100 with two decimal places.
💡 Why This Matters
🌍 Real World
Retail and sales databases often need to show how each product contributes to total sales. This helps managers understand product performance.
💼 Career
Knowing how to use window functions to calculate percentages and totals is a key skill for data analysts and database developers.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns product as text and amount as integer. Then insert these exact rows: ('Apples', 100), ('Bananas', 150), ('Cherries', 50), ('Dates', 200), ('Elderberries', 100).
SQL
Hint
Use CREATE TABLE to make the table and INSERT INTO to add rows.
2
Add a query to select product and amount
Write a SQL SELECT statement to get the product and amount columns from the sales table.
SQL
Hint
Use SELECT product, amount FROM sales; to get the data.
3
Calculate total sales using a window function
Modify the SELECT statement to add a new column called total_sales that uses the window function SUM(amount) OVER () to calculate the total sales amount for all products.
SQL
Hint
Use SUM(amount) OVER () AS total_sales to get the total sales for all rows.
4
Calculate percent of total sales
Update the SELECT statement to add a new column called percent_of_total. Calculate it by dividing amount by SUM(amount) OVER () and multiplying by 100. Use ROUND(..., 2) to round the result to two decimal places.
SQL
Hint
Calculate percent by dividing amount by total sales and multiply by 100. Use ROUND(..., 2) to keep two decimals.
Practice
(1/5)
1. What does the SQL window function SUM(column) OVER () compute in a query?
easy
A. The maximum value of the column
B. The total sum of the column across all rows without grouping
C. The average of the column values
D. The sum of the column for each group defined by GROUP BY
Solution
Step 1: Understand window function syntax
The syntax SUM(column) OVER () means sum over all rows without partitioning or grouping.
Step 2: Compare with GROUP BY sum
Unlike GROUP BY, this sums the entire column across all rows, returning the same total for each row.
Final Answer:
The total sum of the column across all rows without grouping -> Option B
Quick Check:
SUM() OVER () = total sum [OK]
Hint: SUM() OVER () gives total sum for all rows, no grouping [OK]
Common Mistakes:
Confusing window sum with GROUP BY sum
Thinking it calculates average
Assuming it returns max value
2. Which of the following SQL queries correctly calculates the percent of total sales per product using window functions?
easy
A. SELECT product, sales, (sales / SUM(sales) OVER ()) * 100 AS percent_total FROM sales_table;
B. SELECT product, sales, (SUM(sales) OVER () / sales) * 100 AS percent_total FROM sales_table;
C. SELECT product, sales, (sales / SUM(sales)) * 100 AS percent_total FROM sales_table GROUP BY product;
D. SELECT product, sales, (sales / COUNT(*) OVER ()) * 100 AS percent_total FROM sales_table;
Solution
Step 1: Identify correct window function usage
SELECT product, sales, (sales / SUM(sales) OVER ()) * 100 AS percent_total FROM sales_table; uses SUM(sales) OVER () to get total sales across all rows, then divides each sales value by this total.
Step 2: Check other options for errors
SELECT product, sales, (SUM(sales) OVER () / sales) * 100 AS percent_total FROM sales_table; divides total by sales (wrong direction). SELECT product, sales, (sales / SUM(sales)) * 100 AS percent_total FROM sales_table GROUP BY product; uses SUM(sales) without OVER(), which requires GROUP BY and won't work as intended. SELECT product, sales, (sales / COUNT(*) OVER ()) * 100 AS percent_total FROM sales_table; divides sales by count of rows, not sum.
Final Answer:
SELECT product, sales, (sales / SUM(sales) OVER ()) * 100 AS percent_total FROM sales_table; -> Option A
Quick Check:
Percent = sales / total sales * 100 [OK]
Hint: Divide sales by SUM() OVER () and multiply by 100 [OK]
Common Mistakes:
Reversing numerator and denominator
Using SUM() without OVER() in window context
Using COUNT() instead of SUM() for total
3. Given the table sales_data with columns product and amount, what is the output of this query?
SELECT product, amount, ROUND((amount / SUM(amount) OVER ()) * 100, 1) AS percent_total FROM sales_data ORDER BY product;
Assume the data: product | amount -------|------- A | 50 B | 30 C | 20
4. You wrote this query to calculate percent of total sales:
SELECT product, sales, (sales / SUM(sales)) * 100 AS percent_total FROM sales_table;
But it gives an error. What is the problem?
medium
A. SUM(sales) requires a GROUP BY clause or window function syntax
B. sales column does not exist
C. Multiplying by 100 is invalid syntax
D. Missing WHERE clause
Solution
Step 1: Analyze use of SUM() without OVER()
SUM(sales) without GROUP BY or window function context causes error because SQL doesn't know how to aggregate without grouping.
Step 2: Correct usage
Use either GROUP BY product with SUM(sales) or use window function SUM(sales) OVER () to get total sum without grouping.
Final Answer:
SUM(sales) requires a GROUP BY clause or window function syntax -> Option A
Quick Check:
SUM() needs GROUP BY or OVER() [OK]
Hint: Use SUM() OVER () or GROUP BY with SUM() [OK]
Common Mistakes:
Ignoring need for GROUP BY with aggregate functions
Assuming multiplication by 100 causes error
Thinking WHERE clause is mandatory here
5. You have a table orders with columns category and amount. Write a query to show each category, total amount per category, and percent of total amount across all categories. Which query is correct?
hard
A. SELECT category, SUM(amount) AS category_total, (SUM(amount) / COUNT(*) OVER ()) * 100 AS percent_total FROM orders GROUP BY category;
B. SELECT category, amount, (amount / SUM(amount) OVER ()) * 100 AS percent_total FROM orders ORDER BY category;
C. SELECT category, SUM(amount) AS category_total, (SUM(amount) / SUM(SUM(amount)) OVER ()) * 100 AS percent_total FROM orders GROUP BY category ORDER BY category;
D. SELECT category, SUM(amount) AS category_total, (SUM(amount) / SUM(amount)) * 100 AS percent_total FROM orders GROUP BY category;
Solution
Step 1: Calculate total per category
Use SUM(amount) GROUP BY category to get total per category.
Step 2: Calculate percent of total across all categories
Use window function SUM(SUM(amount)) OVER () to get grand total across all categories, then divide category total by this and multiply by 100.
Step 3: Verify query correctness
SELECT category, SUM(amount) AS category_total, (SUM(amount) / SUM(SUM(amount)) OVER ()) * 100 AS percent_total FROM orders GROUP BY category ORDER BY category; correctly combines GROUP BY with window function for total sum. SELECT category, amount, (amount / SUM(amount) OVER ()) * 100 AS percent_total FROM orders ORDER BY category; uses amount per row, not category total. SELECT category, SUM(amount) AS category_total, (SUM(amount) / COUNT(*) OVER ()) * 100 AS percent_total FROM orders GROUP BY category; incorrectly uses COUNT() for total. SELECT category, SUM(amount) AS category_total, (SUM(amount) / SUM(amount)) * 100 AS percent_total FROM orders GROUP BY category; divides sum by itself, always 100%.
Final Answer:
SELECT category, SUM(amount) AS category_total, (SUM(amount) / SUM(SUM(amount)) OVER ()) * 100 AS percent_total FROM orders GROUP BY category ORDER BY category; -> Option C
Quick Check:
Percent = category sum / total sum * 100 [OK]
Hint: Use GROUP BY with SUM() and window SUM(SUM(amount)) OVER () for total [OK]
Common Mistakes:
Using row amount instead of category sum
Using COUNT() instead of SUM() for total
Dividing sum by itself always gives 100%
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.