0
0
MySQLquery~20 mins

GROUP BY with multiple columns in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of GROUP BY with multiple columns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GROUP BY with two columns
Given the table Sales with columns region, product, and amount, what is the output of this query?
SELECT region, product, SUM(amount) AS total_sales
FROM Sales
GROUP BY region, product
ORDER BY region, product;
MySQL
CREATE TABLE Sales (region VARCHAR(10), product VARCHAR(10), amount INT);
INSERT INTO Sales VALUES
('North', 'A', 100),
('North', 'B', 150),
('South', 'A', 200),
('South', 'B', 100),
('North', 'A', 50);
A[{"region": "North", "product": "A", "total_sales": 100}, {"region": "North", "product": "B", "total_sales": 150}, {"region": "South", "product": "A", "total_sales": 200}, {"region": "South", "product": "B", "total_sales": 100}]
B[{"region": "North", "product": "A", "total_sales": 150}, {"region": "North", "product": "B", "total_sales": 250}, {"region": "South", "product": "A", "total_sales": 200}, {"region": "South", "product": "B", "total_sales": 100}]
C[{"region": "North", "product": "A", "total_sales": 150}, {"region": "North", "product": "B", "total_sales": 150}, {"region": "South", "product": "A", "total_sales": 200}, {"region": "South", "product": "B", "total_sales": 100}]
D[{"region": "North", "product": "A", "total_sales": 150}, {"region": "South", "product": "A", "total_sales": 200}]
Attempts:
2 left
💡 Hint
Remember that GROUP BY groups rows by all listed columns together.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in GROUP BY with multiple columns
Which option contains a syntax error in the GROUP BY clause when grouping by multiple columns?
MySQL
SELECT region, product, COUNT(*) FROM Sales GROUP BY region product;
AGROUP BY region product
BGROUP BY region, product
CGROUP BY (region, product)
DGROUP BY region; product
Attempts:
2 left
💡 Hint
Multiple columns in GROUP BY must be separated by commas.
optimization
advanced
2:30remaining
Optimizing GROUP BY with multiple columns
You have a large table Orders with columns customer_id, order_date, and amount. You run this query:
SELECT customer_id, YEAR(order_date) AS order_year, SUM(amount) FROM Orders GROUP BY customer_id, order_year;

Which index would best improve the performance of this query?
AINDEX(customer_id, order_date)
BINDEX(order_date, customer_id)
CINDEX(customer_id, YEAR(order_date))
DINDEX(order_year, customer_id)
Attempts:
2 left
💡 Hint
Indexes on columns used in GROUP BY help, but functions on columns cannot be indexed directly.
🔧 Debug
advanced
2:00remaining
Debugging unexpected GROUP BY results with multiple columns
A developer runs this query:
SELECT department, role, COUNT(*) FROM Employees GROUP BY department;

But the result shows only one role per department, not all roles. Why?
ABecause the query needs a HAVING clause to filter roles.
BBecause COUNT(*) counts only distinct roles, not all employees.
CBecause department is not a valid column for grouping.
DBecause role is not in the GROUP BY clause, MySQL picks an arbitrary role per department.
Attempts:
2 left
💡 Hint
When grouping by fewer columns than selected, non-aggregated columns may show unexpected values.
🧠 Conceptual
expert
3:00remaining
Understanding GROUP BY with multiple columns and NULL values
Consider a table Visits with columns user_id, page, and visit_date. Some page values are NULL. What happens when you run:
SELECT user_id, page, COUNT(*) FROM Visits GROUP BY user_id, page;

How are NULL values in page treated in grouping?
AEach NULL in page is treated as a distinct group, so multiple groups per user_id may have NULL page.
BAll rows with NULL in page are grouped together as one group per user_id.
CRows with NULL page are excluded from the result.
DThe query raises an error because NULL cannot be grouped.
Attempts:
2 left
💡 Hint
In SQL, NULLs are considered equal for grouping purposes.