Challenge - 5 Problems
Aggregation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this aggregation query?
Consider a table Sales with columns
product_id and amount. What does this query return?SELECT product_id, SUM(amount) FROM Sales GROUP BY product_id;
MySQL
CREATE TABLE Sales (product_id INT, amount INT); INSERT INTO Sales VALUES (1, 100), (1, 150), (2, 200), (2, 50), (3, 300);
Attempts:
2 left
💡 Hint
Aggregation sums all amounts per product_id.
✗ Incorrect
The query groups rows by product_id and sums the amount for each group. For product_id 1, amounts 100 + 150 = 250; for 2, 200 + 50 = 250; for 3, 300 alone.
🧠 Conceptual
intermediate1:30remaining
Why do we use aggregation functions in databases?
Which of the following best explains why aggregation functions like SUM, COUNT, and AVG are used in databases?
Attempts:
2 left
💡 Hint
Think about what happens when you want to know totals or averages.
✗ Incorrect
Aggregation functions summarize many rows into one value, making it easier to understand overall data trends.
📝 Syntax
advanced2:00remaining
Identify the correct aggregation query syntax
Which query correctly calculates the average salary per department from a table
Employees(department, salary)?Attempts:
2 left
💡 Hint
Remember to use GROUP BY when aggregating per group.
✗ Incorrect
Option C uses correct syntax: AVG function with GROUP BY to get average salary per department. Option C has wrong function syntax. Option C misses GROUP BY, so invalid. Option C selects only AVG without department column.
🔧 Debug
advanced2:00remaining
Why does this aggregation query cause an error?
Given a table
Orders(order_id, customer_id, amount), why does this query fail?SELECT order_id, customer_id, SUM(amount) FROM Orders GROUP BY customer_id;
Attempts:
2 left
💡 Hint
Check which columns appear in SELECT but not in GROUP BY or aggregation.
✗ Incorrect
SQL requires all selected columns to be either aggregated or included in GROUP BY. order_id is neither, causing an error.
❓ optimization
expert3:00remaining
Optimizing aggregation on large datasets
You have a large
Transactions table with millions of rows. Which approach best improves performance when calculating total sales per region?Attempts:
2 left
💡 Hint
Indexes help queries filter and group data faster.
✗ Incorrect
Indexing the grouping column (region) speeds up grouping and aggregation by allowing quick data access.