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
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember that GROUP BY groups rows by all listed columns together.
✗ Incorrect
The query groups rows by both region and product. For example, 'North' and 'A' rows are combined, summing amounts 100 + 50 = 150.
📝 Syntax
intermediate1: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;
Attempts:
2 left
💡 Hint
Multiple columns in GROUP BY must be separated by commas.
✗ Incorrect
Option A misses the comma between columns, causing a syntax error.
❓ optimization
advanced2:30remaining
Optimizing GROUP BY with multiple columns
You have a large table
Which index would best improve the performance of this query?
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?
Attempts:
2 left
💡 Hint
Indexes on columns used in GROUP BY help, but functions on columns cannot be indexed directly.
✗ Incorrect
You cannot index the result of a function like YEAR(order_date) directly. Indexing customer_id and order_date helps MySQL optimize grouping.
🔧 Debug
advanced2:00remaining
Debugging unexpected GROUP BY results with multiple columns
A developer runs this query:
But the result shows only one role per department, not all roles. Why?
SELECT department, role, COUNT(*) FROM Employees GROUP BY department;
But the result shows only one role per department, not all roles. Why?
Attempts:
2 left
💡 Hint
When grouping by fewer columns than selected, non-aggregated columns may show unexpected values.
✗ Incorrect
Selecting columns not in GROUP BY without aggregation causes MySQL to pick an arbitrary value from that group.
🧠 Conceptual
expert3:00remaining
Understanding GROUP BY with multiple columns and NULL values
Consider a table
How are NULL values in
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?Attempts:
2 left
💡 Hint
In SQL, NULLs are considered equal for grouping purposes.
✗ Incorrect
GROUP BY treats NULL values as equal, so all NULLs in page for the same user_id form one group.