0
0
SQLquery~20 mins

How GROUP BY changes query execution in SQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GROUP BY Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
ASyntaxError
B[{"Region": "East", "SUM(Amount)": 100}, {"Region": "West", "SUM(Amount)": 200}, {"Region": "East", "SUM(Amount)": 150}, {"Region": "West", "SUM(Amount)": 50}]
C[{"Region": "East", "SUM(Amount)": 250}, {"Region": "West", "SUM(Amount)": 250}]
D[{"Region": null, "SUM(Amount)": 500}]
Attempts:
2 left
💡 Hint
GROUP BY groups rows by the column values before applying aggregate functions.
🧠 Conceptual
intermediate
1: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?
AThe query returns the first value of that column for each group.
BThe query returns an error because all selected columns must be grouped or aggregated.
CThe query returns the last value of that column for each group.
DThe query ignores the column and returns only grouped columns.
Attempts:
2 left
💡 Hint
Think about SQL rules for grouping and selecting columns.
📝 Syntax
advanced
2: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)
ASELECT Department, Salary FROM Employees GROUP BY Department;
BSELECT Department, AVG(Salary) FROM Employees GROUP BY Department;
CSELECT Department, COUNT(*) FROM Employees GROUP BY Department;
DSELECT Department, MAX(Salary) FROM Employees GROUP BY Department;
Attempts:
2 left
💡 Hint
Check if all selected columns are grouped or aggregated.
optimization
advanced
1:30remaining
How GROUP BY affects query performance
Which statement best explains how adding a GROUP BY clause affects query execution?
AGROUP BY has no effect on query execution time.
BGROUP BY always makes queries faster by reducing the number of rows returned.
CGROUP BY eliminates the need for indexes on the grouped columns.
DGROUP BY causes the database to sort or hash rows to group them, which can increase execution time.
Attempts:
2 left
💡 Hint
Think about what the database must do to group rows.
🔧 Debug
expert
2:30remaining
Diagnose the cause of unexpected GROUP BY results
A query:
SELECT Department, COUNT(EmployeeID) FROM Employees GROUP BY Department;

returns lower counts than expected. Which issue is most likely causing this?
ACOUNT(EmployeeID) counts only non-NULL EmployeeID, so some rows are excluded.
BSome Department values are NULL, causing those rows to be grouped together.
CThe GROUP BY clause is missing a column, causing rows to merge incorrectly.
DThe query is missing a WHERE clause to filter rows.
Attempts:
2 left
💡 Hint
Consider how COUNT behaves with NULL values.