0
0
MySQLquery~10 mins

GROUP BY with multiple columns in MySQL - 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 both city and product.

MySQL
SELECT city, product, SUM(sales) FROM sales_data GROUP BY [1];
Drag options to blanks, or click blank then click option'
Acity, product
Bproduct
Csales
Dcity
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by only one column when two are needed.
Using the sales column in GROUP BY instead of city and product.
2fill in blank
medium

Complete the code to select city and product, and count the number of sales entries for each group.

MySQL
SELECT city, product, COUNT(*) FROM sales_data GROUP BY [1];
Drag options to blanks, or click blank then click option'
Acity, product
Bcity
Csales
Dproduct
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by only one column and getting incorrect counts.
Using COUNT(column) instead of COUNT(*) which counts all rows.
3fill in blank
hard

Fix the error in the GROUP BY clause to correctly group by city and product.

MySQL
SELECT city, product, AVG(price) FROM sales_data GROUP BY [1];
Drag options to blanks, or click blank then click option'
Aprice
Bcity product
Cproduct city
Dcity, product
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting commas between columns in GROUP BY.
Grouping by a column not in the SELECT list.
4fill in blank
hard

Fill both blanks to group sales by city and product and order the results by city ascending.

MySQL
SELECT city, product, SUM(sales) FROM sales_data GROUP BY [1] ORDER BY [2] ASC;
Drag options to blanks, or click blank then click option'
Acity, product
Bsales
Ccity
Dproduct
Attempts:
3 left
💡 Hint
Common Mistakes
Ordering by a column not selected or grouped.
Grouping by only one column when two are needed.
5fill in blank
hard

Fill all three blanks to select city and product, count sales entries, and filter groups having more than 5 entries.

MySQL
SELECT [1], [2], COUNT(*) FROM sales_data GROUP BY [3] HAVING COUNT(*) > 5;
Drag options to blanks, or click blank then click option'
Acity
Bproduct
Ccity, product
Dsales
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE instead of HAVING to filter aggregated results.
Not grouping by all selected columns.