Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the GROUP BY clause do in an SQL query?
easy
A. It deletes duplicate rows from the table.
B. It sorts the rows in ascending order.
C. It groups rows that have the same values in a specified column.
D. It filters rows based on a condition.

Solution

  1. Step 1: Understand the purpose of GROUP BY

    The GROUP BY clause collects rows with the same value in the specified column into groups.
  2. Step 2: Differentiate from other clauses

    Unlike ORDER BY which sorts, or WHERE which filters, GROUP BY organizes data for aggregation.
  3. Final Answer:

    It groups rows that have the same values in a specified column. -> Option C
  4. Quick Check:

    GROUP BY = grouping rows by column [OK]
Hint: GROUP BY collects rows sharing column values [OK]
Common Mistakes:
  • Confusing GROUP BY with ORDER BY
  • Thinking GROUP BY filters rows
  • Assuming GROUP BY deletes duplicates
2. Which of the following is the correct syntax to group data by the column department?
easy
A. SELECT department, COUNT(*) FROM employees GROUP BY department;
B. SELECT department, COUNT(*) FROM employees ORDER BY department;
C. SELECT department, COUNT(*) FROM employees WHERE department;
D. SELECT department, COUNT(*) FROM employees HAVING department;

Solution

  1. Step 1: Identify correct GROUP BY usage

    The GROUP BY clause must follow FROM and group by the column named.
  2. Step 2: Check each option's clause

    SELECT department, COUNT(*) FROM employees GROUP BY department; uses GROUP BY correctly; others use ORDER BY, WHERE, HAVING incorrectly here.
  3. Final Answer:

    SELECT department, COUNT(*) FROM employees GROUP BY department; -> Option A
  4. Quick Check:

    GROUP BY syntax = SELECT ... GROUP BY column [OK]
Hint: GROUP BY follows FROM and groups by column [OK]
Common Mistakes:
  • Using ORDER BY instead of GROUP BY
  • Using WHERE to group data
  • Using HAVING without aggregation
3. 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;
medium
A. A list of all sales amounts without grouping.
B. A list of regions with the total sales amount for each region.
C. An error because SUM() cannot be used with GROUP BY.
D. A list of regions sorted by amount.

Solution

  1. Step 1: Understand GROUP BY with SUM()

    The query groups rows by region and sums the amount for each group.
  2. Step 2: Predict output format

    The output shows each region once with the total amount of sales in that region.
  3. Final Answer:

    A list of regions with the total sales amount for each region. -> Option B
  4. Quick Check:

    GROUP BY + SUM() = grouped sums [OK]
Hint: GROUP BY + SUM() gives totals per group [OK]
Common Mistakes:
  • Thinking SUM() can't be used with GROUP BY
  • Expecting ungrouped list
  • Confusing sorting with grouping
4. Identify the error in this SQL query:
SELECT department, COUNT(*) FROM employees;
medium
A. The query is correct and will run without errors.
B. COUNT(*) cannot be used without WHERE clause.
C. department cannot be selected without aggregation.
D. Missing GROUP BY clause for the department column.

Solution

  1. Step 1: Check SELECT with aggregation

    COUNT(*) is an aggregate but department is not aggregated or grouped.
  2. Step 2: Identify missing GROUP BY

    To select department with COUNT(*), GROUP BY department is required.
  3. Final Answer:

    Missing GROUP BY clause for the department column. -> Option D
  4. Quick Check:

    Non-aggregated columns need GROUP BY [OK]
Hint: Non-aggregated columns need GROUP BY [OK]
Common Mistakes:
  • Ignoring missing GROUP BY
  • Thinking COUNT(*) needs WHERE
  • Assuming query runs without error
5. You have a table orders with columns customer_id, order_date, and total. You want to find the average order total per customer but only for customers who have placed more than 3 orders. Which query correctly achieves this?
hard
A. SELECT customer_id, AVG(total) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3;
B. SELECT customer_id, AVG(total) FROM orders WHERE COUNT(*) > 3 GROUP BY customer_id;
C. SELECT customer_id, AVG(total) FROM orders GROUP BY customer_id WHERE COUNT(*) > 3;
D. SELECT customer_id, AVG(total) FROM orders HAVING COUNT(*) > 3 GROUP BY customer_id;

Solution

  1. Step 1: Use GROUP BY to group orders by customer_id

    This groups all orders per customer to calculate aggregates.
  2. Step 2: Use HAVING to filter groups with more than 3 orders

    HAVING filters groups after aggregation; WHERE cannot filter aggregates.
  3. Final Answer:

    SELECT customer_id, AVG(total) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; -> Option A
  4. Quick Check:

    HAVING filters groups, WHERE filters rows [OK]
Hint: Use HAVING to filter groups after GROUP BY [OK]
Common Mistakes:
  • Using WHERE to filter aggregated counts
  • Placing HAVING before GROUP BY
  • Not filtering groups at all