Challenge - 5 Problems
GROUP BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of GROUP BY on a single column
Given the table Sales with columns
Product and Quantity, what is the output of the following query?SELECT Product, SUM(Quantity) FROM Sales GROUP BY Product;
SQL
CREATE TABLE Sales (Product VARCHAR(20), Quantity INT); INSERT INTO Sales VALUES ('Apple', 10), ('Banana', 5), ('Apple', 15), ('Banana', 10), ('Cherry', 7);
Attempts:
2 left
💡 Hint
GROUP BY combines rows with the same value in the grouped column and aggregates other columns.
✗ Incorrect
The query groups rows by Product and sums the Quantity for each product. Apple has 10 + 15 = 25, Banana has 5 + 10 = 15, Cherry has 7.
🧠 Conceptual
intermediate2:00remaining
Understanding GROUP BY behavior
What happens if you run this query?
Assuming the same
SELECT Product, Quantity FROM Sales GROUP BY Product;
Assuming the same
Sales table as before.Attempts:
2 left
💡 Hint
In SQL, columns in SELECT must be aggregated or included in GROUP BY.
✗ Incorrect
SQL requires that all selected columns be either grouped or aggregated. Quantity is neither, so this query causes an error.
📝 Syntax
advanced2:00remaining
Identify the syntax error in GROUP BY query
Which option contains a syntax error when grouping by a single column?
SQL
Table: Orders(OrderID INT, Customer VARCHAR(20), Amount INT)
Attempts:
2 left
💡 Hint
Check the syntax of the GROUP BY clause.
✗ Incorrect
Option C is missing the keyword BY after GROUP, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing GROUP BY query performance
You have a large table
Transactions with columns UserID, Amount, and Date. You want to get total amount per user quickly. Which option improves query speed the most?Attempts:
2 left
💡 Hint
Indexes help speed up grouping on the indexed column.
✗ Incorrect
Indexing UserID helps the database quickly group rows by UserID, improving performance.
🔧 Debug
expert2:00remaining
Why does this GROUP BY query return fewer rows than expected?
Given the table
You notice some departments are missing in the result. What is the most likely reason?
Employees with columns Department and Salary, you run:SELECT Department, COUNT(*) FROM Employees GROUP BY Department;
You notice some departments are missing in the result. What is the most likely reason?
Attempts:
2 left
💡 Hint
Think about how NULL values behave in GROUP BY.
✗ Incorrect
GROUP BY treats NULL as a group, but some databases may exclude NULL groups or they appear as NULL. Departments with NULL values might be missing or grouped separately.