0
0
SQLquery~10 mins

Running totals with SUM OVER in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate a running total of sales ordered by date.

SQL
SELECT date, sales, SUM(sales) OVER (ORDER BY [1]) AS running_total FROM sales_data;
Drag options to blanks, or click blank then click option'
Aregion
Bsales
Ccustomer_id
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Ordering by sales instead of date causes incorrect running totals.
Using a column unrelated to time order breaks the running total logic.
2fill in blank
medium

Complete the code to reset the running total for each region.

SQL
SELECT region, date, sales, SUM(sales) OVER (PARTITION BY [1] ORDER BY date) AS running_total FROM sales_data;
Drag options to blanks, or click blank then click option'
Aregion
Bdate
Csales
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Partitioning by date resets the total every day, not by region.
Not using PARTITION BY causes a running total over all data.
3fill in blank
hard

Fix the error in the code to correctly calculate the running total of sales per customer ordered by date.

SQL
SELECT customer_id, date, sales, SUM(sales) OVER (PARTITION BY customer_id ORDER BY [1]) AS running_total FROM sales_data;
Drag options to blanks, or click blank then click option'
Adate
Bsales
Cregion
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Ordering by sales causes running totals to jump incorrectly.
Ordering by customer_id inside partition by customer_id is redundant and wrong.
4fill in blank
hard

Fill both blanks to calculate a running total of sales per region, ordered by date, and include only sales greater than 100.

SQL
SELECT region, date, sales, SUM(sales) OVER (PARTITION BY [1] ORDER BY [2]) AS running_total FROM sales_data WHERE sales > 100;
Drag options to blanks, or click blank then click option'
Aregion
Bdate
Csales
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping partition and order columns causes wrong grouping or ordering.
Not filtering sales before running total changes results.
5fill in blank
hard

Fill all three blanks to calculate a running total of sales per customer and region, ordered by date, including only sales in 2023.

SQL
SELECT customer_id, region, date, sales, SUM(sales) OVER (PARTITION BY [1], [2] ORDER BY [3]) AS running_total FROM sales_data WHERE EXTRACT(year FROM date) = 2023;
Drag options to blanks, or click blank then click option'
Acustomer_id
Bregion
Cdate
Dsales
Attempts:
3 left
💡 Hint
Common Mistakes
Including sales in partition or order causes errors.
Not filtering by year 2023 includes unwanted data.