0
0
SQLquery~10 mins

Window function vs GROUP BY mental model in SQL - Interactive Practice

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

Complete the code to calculate the total sales per product using GROUP BY.

SQL
SELECT product_id, SUM(sales) AS total_sales FROM sales_data GROUP BY [1];
Drag options to blanks, or click blank then click option'
Asales
Bproduct_id
Cdate
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by sales instead of product_id.
Forgetting to use GROUP BY when using aggregate functions.
2fill in blank
medium

Complete the code to calculate the running total of sales per product using a window function.

SQL
SELECT product_id, 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'
Adate
Bsales
Ccustomer_id
Dproduct_id
Attempts:
3 left
💡 Hint
Common Mistakes
Partitioning by sales or date instead of product_id.
Confusing PARTITION BY with GROUP BY.
3fill in blank
hard

Fix the error in the query to calculate average sales per product using GROUP BY.

SQL
SELECT product_id, AVG(sales) FROM sales_data WHERE sales > 0 GROUP BY [1];
Drag options to blanks, or click blank then click option'
Adate
Bsales
Cproduct_id
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by sales instead of product_id.
Omitting GROUP BY clause.
4fill in blank
hard

Fill both blanks to calculate the rank of sales per product ordered by date.

SQL
SELECT product_id, sales, RANK() OVER (PARTITION BY [1] ORDER BY [2]) AS sales_rank FROM sales_data;
Drag options to blanks, or click blank then click option'
Aproduct_id
Bsales
Cdate
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using sales for partition instead of product_id.
Ordering by sales instead of date.
5fill in blank
hard

Fill all three blanks to create a query that shows total sales per product and each sale's percentage of that total.

SQL
SELECT product_id, sales, SUM(sales) OVER (PARTITION BY [1]) AS total_sales, ROUND(sales * 100.0 / [2], 2) AS sales_percent FROM sales_data;
Drag options to blanks, or click blank then click option'
Aproduct_id
BSUM(sales) OVER (PARTITION BY product_id)
Ctotal_sales
Dsales
Attempts:
3 left
💡 Hint
Common Mistakes
Using sales instead of product_id for partition.
Dividing by sales instead of total_sales.