0
0
MySQLquery~20 mins

GROUP BY clause in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GROUP BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GROUP BY with aggregate function
Given the table Sales with columns product and quantity, what is the output of this query?
SELECT product, SUM(quantity) FROM Sales GROUP BY product;
MySQL
CREATE TABLE Sales (product VARCHAR(20), quantity INT);
INSERT INTO Sales VALUES ('apple', 10), ('banana', 5), ('apple', 15), ('banana', 10), ('orange', 7);
A[{"product": "apple", "SUM(quantity)": 25}, {"product": "banana", "SUM(quantity)": 15}, {"product": "orange", "SUM(quantity)": 7}]
B[{"product": "apple", "SUM(quantity)": 10}, {"product": "banana", "SUM(quantity)": 5}, {"product": "orange", "SUM(quantity)": 7}]
C[{"product": "apple", "SUM(quantity)": 25}, {"product": "banana", "SUM(quantity)": 15}]
D[{"product": "apple", "SUM(quantity)": 25}, {"product": "banana", "SUM(quantity)": 15}, {"product": "orange", "SUM(quantity)": 0}]
Attempts:
2 left
💡 Hint
GROUP BY groups rows by product and SUM adds quantities per group.
🧠 Conceptual
intermediate
1:30remaining
Understanding GROUP BY with multiple columns
Consider a table Orders with columns customer_id, product, and quantity. What does this query do?
SELECT customer_id, product, COUNT(*) FROM Orders GROUP BY customer_id, product;
ACounts total orders for each product regardless of customer.
BCounts total orders for each customer regardless of product.
CCounts how many orders each customer made for each product.
DCounts total orders in the table without grouping.
Attempts:
2 left
💡 Hint
GROUP BY with two columns groups rows by unique pairs of those columns.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in GROUP BY usage
Which option contains a syntax error in the GROUP BY clause?
SELECT department, AVG(salary) FROM Employees GROUP BY;
ASELECT department, AVG(salary) FROM Employees GROUP BY department;
BSELECT department, AVG(salary) FROM Employees GROUP BY;
CSELECT department, AVG(salary) FROM Employees GROUP BY department ORDER BY salary;
DSELECT department, AVG(salary) FROM Employees GROUP BY department, salary;
Attempts:
2 left
💡 Hint
GROUP BY must specify at least one column to group by.
optimization
advanced
2:00remaining
Optimizing GROUP BY with indexes
Which index will best improve performance of this query?
SELECT city, COUNT(*) FROM Customers GROUP BY city;
ACREATE INDEX idx_city ON Customers(city);
BCREATE INDEX idx_name ON Customers(name);
CCREATE INDEX idx_city_name ON Customers(city, name);
DCREATE INDEX idx_id ON Customers(id);
Attempts:
2 left
💡 Hint
Indexes on columns used in GROUP BY help speed grouping.
🔧 Debug
expert
2:30remaining
Why does this GROUP BY query fail?
Given the table Employees with columns id, department, and salary, why does this query cause an error?
SELECT id, department, AVG(salary) FROM Employees GROUP BY department;
ABecause AVG(salary) cannot be used with GROUP BY.
BBecause GROUP BY must include all columns in the table.
CBecause department is not in SELECT clause.
DBecause id is not in GROUP BY or an aggregate function.
Attempts:
2 left
💡 Hint
Columns in SELECT must be in GROUP BY or aggregated.