0
0
SQLquery~20 mins

GROUP BY single column in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GROUP BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
A[{"Product": "Apple", "SUM(Quantity)": 25}, {"Product": "Banana", "SUM(Quantity)": 15}, {"Product": "Cherry", "SUM(Quantity)": 7}]
B[{"Product": "Apple", "SUM(Quantity)": 10}, {"Product": "Banana", "SUM(Quantity)": 5}, {"Product": "Apple", "SUM(Quantity)": 15}, {"Product": "Banana", "SUM(Quantity)": 10}, {"Product": "Cherry", "SUM(Quantity)": 7}]
C[{"Product": "Apple", "SUM(Quantity)": 25}, {"Product": "Banana", "SUM(Quantity)": 15}]
D[{"Product": "Apple", "SUM(Quantity)": 10}, {"Product": "Banana", "SUM(Quantity)": 15}, {"Product": "Cherry", "SUM(Quantity)": 7}]
Attempts:
2 left
💡 Hint
GROUP BY combines rows with the same value in the grouped column and aggregates other columns.
🧠 Conceptual
intermediate
2:00remaining
Understanding GROUP BY behavior
What happens if you run this query?
SELECT Product, Quantity FROM Sales GROUP BY Product;

Assuming the same Sales table as before.
AIt returns one row per product with a random Quantity value from that product's rows.
BIt returns an error because Quantity is not aggregated or in GROUP BY.
CIt sums the Quantity for each product automatically.
DIt returns all rows without grouping.
Attempts:
2 left
💡 Hint
In SQL, columns in SELECT must be aggregated or included in GROUP BY.
📝 Syntax
advanced
2: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)
ASELECT Customer, SUM(Amount) FROM Orders GROUP BY Customer;
BSELECT Customer, SUM(Amount) FROM Orders GROUP BY Customer ORDER BY Customer;
CSELECT Customer, SUM(Amount) FROM Orders GROUP Customer;
DSELECT Customer, SUM(Amount) FROM Orders GROUP BY Customer HAVING SUM(Amount) > 100;
Attempts:
2 left
💡 Hint
Check the syntax of the GROUP BY clause.
optimization
advanced
2: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?
AAdd an index on UserID and run: SELECT UserID, SUM(Amount) FROM Transactions GROUP BY UserID;
BAdd an index on Amount and run: SELECT UserID, SUM(Amount) FROM Transactions GROUP BY UserID;
CRun: SELECT UserID, SUM(Amount) FROM Transactions;
DRun: SELECT UserID, SUM(Amount) FROM Transactions GROUP BY UserID ORDER BY UserID;
Attempts:
2 left
💡 Hint
Indexes help speed up grouping on the indexed column.
🔧 Debug
expert
2:00remaining
Why does this GROUP BY query return fewer rows than expected?
Given the table 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?
AGROUP BY only shows departments with the highest salary.
BThe COUNT(*) function filters out departments with zero employees.
CThe query needs a WHERE clause to include all departments.
DSome departments have NULL values in Department column, and GROUP BY excludes NULL groups.
Attempts:
2 left
💡 Hint
Think about how NULL values behave in GROUP BY.