Challenge - 5 Problems
Grouping Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of GROUP BY in SQL
Why do we use the GROUP BY clause in SQL queries?
Attempts:
2 left
💡 Hint
Think about how to summarize data by categories.
✗ Incorrect
The GROUP BY clause groups rows that have the same values in specified columns. This allows aggregate functions like COUNT, SUM, AVG to be applied to each group, producing summary results.
❓ query_result
intermediate2:00remaining
Output of GROUP BY with COUNT
Given the table Sales with columns
Product and Quantity, what is the output of this query?SELECT Product, COUNT(*) FROM Sales GROUP BY Product;
SQL
Sales table data: Product | Quantity --------|--------- Apple | 10 Banana | 5 Apple | 7 Banana | 3 Cherry | 8
Attempts:
2 left
💡 Hint
COUNT(*) counts rows per group, not sum of Quantity.
✗ Incorrect
The query groups rows by Product and counts how many rows each product has. Apple appears twice, Banana twice, Cherry once.
📝 Syntax
advanced1:30remaining
Identify the syntax error in GROUP BY usage
Which option contains a syntax error in using GROUP BY?
SQL
Table: Employees (Name, Department, Salary)Attempts:
2 left
💡 Hint
Check if all selected columns are either grouped or aggregated.
✗ Incorrect
Option C selects Name and Department but groups only by Department. Name is neither aggregated nor grouped, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing GROUP BY queries with indexes
Which index will best improve performance of this query?
SELECT Department, COUNT(*) FROM Employees GROUP BY Department;
Attempts:
2 left
💡 Hint
Think about which column is used in GROUP BY.
✗ Incorrect
Indexing the column used in GROUP BY (Department) helps the database group rows faster.
🔧 Debug
expert2:30remaining
Why does this GROUP BY query fail?
Given the table Orders with columns
OrderID, CustomerID, and OrderDate, why does this query fail?SELECT CustomerID, OrderDate, COUNT(*) FROM Orders GROUP BY CustomerID;
Attempts:
2 left
💡 Hint
Check if all selected columns are grouped or aggregated.
✗ Incorrect
OrderDate is selected but not grouped or aggregated, causing the query to fail.