Bird
Raised Fist0
MySQLquery~20 mins

GROUP BY with multiple columns in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of GROUP BY with multiple columns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of GROUP BY with two columns
Given the table Sales with columns region, product, and amount, what is the output of this query?
SELECT region, product, SUM(amount) AS total_sales
FROM Sales
GROUP BY region, product
ORDER BY region, product;
MySQL
CREATE TABLE Sales (region VARCHAR(10), product VARCHAR(10), amount INT);
INSERT INTO Sales VALUES
('North', 'A', 100),
('North', 'B', 150),
('South', 'A', 200),
('South', 'B', 100),
('North', 'A', 50);
A[{"region": "North", "product": "A", "total_sales": 100}, {"region": "North", "product": "B", "total_sales": 150}, {"region": "South", "product": "A", "total_sales": 200}, {"region": "South", "product": "B", "total_sales": 100}]
B[{"region": "North", "product": "A", "total_sales": 150}, {"region": "North", "product": "B", "total_sales": 250}, {"region": "South", "product": "A", "total_sales": 200}, {"region": "South", "product": "B", "total_sales": 100}]
C[{"region": "North", "product": "A", "total_sales": 150}, {"region": "North", "product": "B", "total_sales": 150}, {"region": "South", "product": "A", "total_sales": 200}, {"region": "South", "product": "B", "total_sales": 100}]
D[{"region": "North", "product": "A", "total_sales": 150}, {"region": "South", "product": "A", "total_sales": 200}]
Attempts:
2 left
💡 Hint
Remember that GROUP BY groups rows by all listed columns together.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in GROUP BY with multiple columns
Which option contains a syntax error in the GROUP BY clause when grouping by multiple columns?
MySQL
SELECT region, product, COUNT(*) FROM Sales GROUP BY region product;
AGROUP BY region product
BGROUP BY region, product
CGROUP BY (region, product)
DGROUP BY region; product
Attempts:
2 left
💡 Hint
Multiple columns in GROUP BY must be separated by commas.
❓ optimization
advanced
2:30remaining
Optimizing GROUP BY with multiple columns
You have a large table Orders with columns customer_id, order_date, and amount. You run this query:
SELECT customer_id, YEAR(order_date) AS order_year, SUM(amount) FROM Orders GROUP BY customer_id, order_year;

Which index would best improve the performance of this query?
AINDEX(customer_id, order_date)
BINDEX(order_date, customer_id)
CINDEX(customer_id, YEAR(order_date))
DINDEX(order_year, customer_id)
Attempts:
2 left
💡 Hint
Indexes on columns used in GROUP BY help, but functions on columns cannot be indexed directly.
🔧 Debug
advanced
2:00remaining
Debugging unexpected GROUP BY results with multiple columns
A developer runs this query:
SELECT department, role, COUNT(*) FROM Employees GROUP BY department;

But the result shows only one role per department, not all roles. Why?
ABecause the query needs a HAVING clause to filter roles.
BBecause COUNT(*) counts only distinct roles, not all employees.
CBecause department is not a valid column for grouping.
DBecause role is not in the GROUP BY clause, MySQL picks an arbitrary role per department.
Attempts:
2 left
💡 Hint
When grouping by fewer columns than selected, non-aggregated columns may show unexpected values.
🧠 Conceptual
expert
3:00remaining
Understanding GROUP BY with multiple columns and NULL values
Consider a table Visits with columns user_id, page, and visit_date. Some page values are NULL. What happens when you run:
SELECT user_id, page, COUNT(*) FROM Visits GROUP BY user_id, page;

How are NULL values in page treated in grouping?
AEach NULL in page is treated as a distinct group, so multiple groups per user_id may have NULL page.
BAll rows with NULL in page are grouped together as one group per user_id.
CRows with NULL page are excluded from the result.
DThe query raises an error because NULL cannot be grouped.
Attempts:
2 left
💡 Hint
In SQL, NULLs are considered equal for grouping purposes.

Practice

(1/5)
1. What does the GROUP BY clause with multiple columns do in a MySQL query?
easy
A. It groups rows based on only the first column listed.
B. It groups rows based on the combination of values in all specified columns.
C. It sorts the rows without grouping them.
D. It filters rows that have duplicate values in any column.

Solution

  1. Step 1: Understand GROUP BY behavior

    The GROUP BY clause groups rows that have the same values in the specified columns together.
  2. Step 2: Multiple columns effect

    When multiple columns are listed, grouping happens on the combined values of all those columns, not individually.
  3. Final Answer:

    It groups rows based on the combination of values in all specified columns. -> Option B
  4. Quick Check:

    GROUP BY multiple columns groups by combined values [OK]
Hint: GROUP BY multiple columns groups by combined column values [OK]
Common Mistakes:
  • Thinking it groups by each column separately
  • Confusing GROUP BY with ORDER BY
  • Assuming it filters duplicates instead of grouping
2. Which of the following is the correct syntax to group data by columns city and department in MySQL?
easy
A. SELECT city, department FROM employees GROUP BY city, department;
B. SELECT city, department FROM employees GROUP city, department;
C. SELECT city, department FROM employees GROUP BY city department;
D. SELECT city, department FROM employees GROUP BY (city, department);

Solution

  1. Step 1: Identify correct GROUP BY syntax

    The correct syntax uses GROUP BY followed by column names separated by commas.
  2. Step 2: Check each option

    SELECT city, department FROM employees GROUP BY city, department; uses correct syntax with commas. Options A, B, and D have syntax errors or wrong keywords.
  3. Final Answer:

    SELECT city, department FROM employees GROUP BY city, department; -> Option A
  4. Quick Check:

    GROUP BY columns separated by commas [OK]
Hint: Use commas to separate columns in GROUP BY [OK]
Common Mistakes:
  • Omitting commas between columns
  • Using wrong keywords like GROUP instead of GROUP BY
  • Using parentheses incorrectly around columns
3. Given the table sales with columns region, product, and amount, what will this query return?
SELECT region, product, SUM(amount) FROM sales GROUP BY region, product;
medium
A. Syntax error due to missing alias for SUM(amount).
B. Total sales amount for each region only.
C. Total sales amount for each product only.
D. Total sales amount for each region and product combination.

Solution

  1. Step 1: Understand GROUP BY with multiple columns

    The query groups rows by both region and product together.
  2. Step 2: Apply SUM aggregation

    Within each group, it sums the amount values, giving total sales per region-product pair.
  3. Final Answer:

    Total sales amount for each region and product combination. -> Option D
  4. Quick Check:

    GROUP BY multiple columns sums per combined group [OK]
Hint: GROUP BY multiple columns sums per combined group [OK]
Common Mistakes:
  • Thinking it sums only by one column
  • Expecting syntax error without alias (alias optional)
  • Confusing GROUP BY with WHERE filtering
4. Identify the error in this query:
SELECT city, department, COUNT(*) FROM employees GROUP BY city;
medium
A. Missing department in GROUP BY causes error or incorrect grouping.
B. COUNT(*) cannot be used with GROUP BY.
C. SELECT must include only aggregated columns.
D. GROUP BY should be after WHERE clause.

Solution

  1. Step 1: Check SELECT and GROUP BY columns

    The query selects city and department but groups only by city.
  2. Step 2: Understand grouping rules

    All non-aggregated columns in SELECT must appear in GROUP BY to avoid errors or unpredictable results.
  3. Final Answer:

    Missing department in GROUP BY causes error or incorrect grouping. -> Option A
  4. Quick Check:

    All selected non-aggregated columns must be in GROUP BY [OK]
Hint: Include all non-aggregated SELECT columns in GROUP BY [OK]
Common Mistakes:
  • Selecting columns not in GROUP BY without aggregation
  • Thinking COUNT(*) is invalid with GROUP BY
  • Misplacing GROUP BY clause
5. You have a transactions table with columns store_id, employee_id, and sales_amount. You want to find the total sales per store and employee but only for employees with total sales above 1000. Which query correctly achieves this?
hard
A. SELECT store_id, employee_id, SUM(sales_amount) FROM transactions GROUP BY store_id HAVING SUM(sales_amount) > 1000;
B. SELECT store_id, employee_id, SUM(sales_amount) FROM transactions WHERE SUM(sales_amount) > 1000 GROUP BY store_id, employee_id;
C. SELECT store_id, employee_id, SUM(sales_amount) FROM transactions GROUP BY store_id, employee_id HAVING SUM(sales_amount) > 1000;
D. SELECT store_id, employee_id, SUM(sales_amount) FROM transactions GROUP BY employee_id, store_id WHERE SUM(sales_amount) > 1000;

Solution

  1. Step 1: Group by both store_id and employee_id

    We need totals per store and employee, so GROUP BY both columns together.
  2. Step 2: Filter groups with HAVING clause

    Use HAVING to filter groups where total sales exceed 1000. WHERE cannot filter aggregated results.
  3. Final Answer:

    SELECT store_id, employee_id, SUM(sales_amount) FROM transactions GROUP BY store_id, employee_id HAVING SUM(sales_amount) > 1000; -> Option C
  4. Quick Check:

    Use HAVING to filter aggregated groups [OK]
Hint: Use HAVING to filter after GROUP BY aggregation [OK]
Common Mistakes:
  • Using WHERE to filter aggregated results
  • Grouping by only one column instead of both
  • Placing WHERE after GROUP BY
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard