0
0
PostgreSQLquery~10 mins

GROUP BY single and multiple columns in PostgreSQL - Interactive Code Practice

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

Complete 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'
Asales
Bquantity
Cproduct_id
DSUM(quantity)
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by quantity instead of product_id.
Using aggregate functions in GROUP BY.
2fill in blank
medium

Complete 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'
Aquantity
Bproduct_id
CSUM(quantity)
Dstore_id
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by quantity or aggregate functions.
Forgetting to include all selected non-aggregated columns in GROUP BY.
3fill in blank
hard

Fix 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'
Acustomer_id
BCOUNT(*)
Corders
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by COUNT(*) which is an aggregate function.
Using table names or * in GROUP BY.
4fill in blank
hard

Fill 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'
Ayear
Bmonth
Cday
Dquarter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' or 'quarter' instead of 'month'.
Not matching the extracted parts with GROUP BY aliases.
5fill in blank
hard

Fill 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'
Adepartment
Bjob_title
Dsalary
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by salary which is aggregated.
Mismatch between SELECT columns and GROUP BY columns.