Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to group the sales by the product_id.
PostgreSQL
SELECT product_id, SUM(quantity) FROM sales GROUP BY [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by quantity instead of product_id.
Using aggregate functions in GROUP BY.
✗ Incorrect
We group by product_id to aggregate sales per product.
2fill in blank
mediumComplete the code to group sales by both product_id and store_id.
PostgreSQL
SELECT product_id, store_id, SUM(quantity) FROM sales GROUP BY [1], store_id; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by quantity or aggregate functions.
Forgetting to include all selected non-aggregated columns in GROUP BY.
✗ Incorrect
We group by both product_id and store_id to get totals per product per store.
3fill in blank
hardFix the error in the GROUP BY clause to correctly group by customer_id.
PostgreSQL
SELECT customer_id, COUNT(*) FROM orders GROUP BY [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by COUNT(*) which is an aggregate function.
Using table names or * in GROUP BY.
✗ Incorrect
The GROUP BY clause must use the column name customer_id, not an aggregate or table name.
4fill in blank
hardFill both blanks to group sales by year and month.
PostgreSQL
SELECT EXTRACT([1] FROM sale_date) AS year, EXTRACT([2] FROM sale_date) AS month, SUM(amount) FROM sales GROUP BY year, month;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' or 'quarter' instead of 'month'.
Not matching the extracted parts with GROUP BY aliases.
✗ Incorrect
We extract year and month from sale_date to group sales by these periods.
5fill in blank
hardFill all three blanks to group employees by department and job title, showing average salary.
PostgreSQL
SELECT [1], [2], AVG(salary) FROM employees GROUP BY [3], job_title;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by salary which is aggregated.
Mismatch between SELECT columns and GROUP BY columns.
✗ Incorrect
We select and group by department and job_title to get average salary per group.