Challenge - 5 Problems
GROUP BY Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of GROUP BY with aggregate function
Given the table Sales with columns
Region and Amount, what is the output of this query?SELECT Region, SUM(Amount) FROM Sales GROUP BY Region;
SQL
CREATE TABLE Sales (Region VARCHAR(10), Amount INT); INSERT INTO Sales VALUES ('East', 100), ('West', 200), ('East', 150), ('West', 50);
Attempts:
2 left
💡 Hint
GROUP BY groups rows by the column values before applying aggregate functions.
✗ Incorrect
The query groups rows by Region and sums Amount for each group. East has 100 + 150 = 250, West has 200 + 50 = 250.
🧠 Conceptual
intermediate1:30remaining
Effect of GROUP BY on non-aggregated columns
What happens if you select a column in a query with GROUP BY that is not included in the GROUP BY clause or an aggregate function?
Attempts:
2 left
💡 Hint
Think about SQL rules for grouping and selecting columns.
✗ Incorrect
SQL requires that all selected columns must be either in the GROUP BY clause or used inside an aggregate function. Otherwise, it raises an error.
📝 Syntax
advanced2:00remaining
Identify the syntax error in GROUP BY usage
Which option contains a syntax error in the GROUP BY clause?
SQL
Table: Employees (Department VARCHAR, Salary INT)
Attempts:
2 left
💡 Hint
Check if all selected columns are grouped or aggregated.
✗ Incorrect
Option A selects Salary without aggregation or grouping, causing a syntax error.
❓ optimization
advanced1:30remaining
How GROUP BY affects query performance
Which statement best explains how adding a GROUP BY clause affects query execution?
Attempts:
2 left
💡 Hint
Think about what the database must do to group rows.
✗ Incorrect
Grouping requires sorting or hashing rows, which adds processing overhead and can slow queries.
🔧 Debug
expert2:30remaining
Diagnose the cause of unexpected GROUP BY results
A query:
returns lower counts than expected. Which issue is most likely causing this?
SELECT Department, COUNT(EmployeeID) FROM Employees GROUP BY Department;
returns lower counts than expected. Which issue is most likely causing this?
Attempts:
2 left
💡 Hint
Consider how COUNT behaves with NULL values.
✗ Incorrect
COUNT(column) counts only rows where the column is not NULL, so rows with NULL EmployeeID are not counted, reducing totals.