0
0
SQLquery~20 mins

GROUP BY multiple columns in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of GROUP BY 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 on 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;
SQL
CREATE TABLE Sales (region VARCHAR(10), product VARCHAR(10), amount INT);
INSERT INTO Sales VALUES
('East', 'Pen', 10),
('East', 'Pen', 15),
('East', 'Pencil', 5),
('West', 'Pen', 20),
('West', 'Pencil', 10);
A[{"region": "East", "product": "Pen", "total_sales": 15}, {"region": "East", "product": "Pencil", "total_sales": 5}, {"region": "West", "product": "Pen", "total_sales": 20}, {"region": "West", "product": "Pencil", "total_sales": 10}]
B[{"region": "East", "product": "Pen", "total_sales": 25}, {"region": "East", "product": "Pencil", "total_sales": 5}, {"region": "West", "product": "Pen", "total_sales": 20}, {"region": "West", "product": "Pencil", "total_sales": 10}]
C[{"region": "East", "product": "Pen", "total_sales": 25}, {"region": "East", "product": "Pencil", "total_sales": 10}, {"region": "West", "product": "Pen", "total_sales": 20}, {"region": "West", "product": "Pencil", "total_sales": 10}]
D[{"region": "East", "product": "Pen", "total_sales": 25}, {"region": "West", "product": "Pen", "total_sales": 20}]
Attempts:
2 left
💡 Hint
GROUP BY groups rows that have the same values in all listed columns.
🧠 Conceptual
intermediate
1:30remaining
Understanding GROUP BY multiple columns
Why do we use multiple columns in a GROUP BY clause in SQL?
ATo join multiple tables based on those columns.
BTo filter rows before grouping them.
CTo group rows that have the same values in all specified columns together.
DTo sort the results after grouping.
Attempts:
2 left
💡 Hint
Think about how grouping works with one column, then extend to multiple columns.
📝 Syntax
advanced
1:30remaining
Identify the correct GROUP BY syntax with multiple columns
Which of the following SQL queries correctly groups by two columns category and year?
ASELECT category, year, COUNT(*) FROM sales GROUP BY category, year;
BSELECT category, year, COUNT(*) FROM sales GROUP BY category year;
CSELECT category, year, COUNT(*) FROM sales GROUP BY (category, year);
DSELECT category, year, COUNT(*) FROM sales GROUP BY category; year;
Attempts:
2 left
💡 Hint
Check the syntax for separating columns in GROUP BY.
optimization
advanced
2:00remaining
Optimizing GROUP BY with multiple columns
You have a large table with columns country, city, and sales. Which index would best improve performance of this query?
SELECT country, city, SUM(sales) FROM sales_data GROUP BY country, city;
ANo index is needed for GROUP BY queries.
BCreate separate indexes on country and city individually.
CCreate an index on sales column.
DCreate a composite index on (country, city).
Attempts:
2 left
💡 Hint
Think about how the database groups rows efficiently.
🔧 Debug
expert
2:30remaining
Why does this GROUP BY query cause an error?
Consider this query:
SELECT department, employee, COUNT(*) FROM employees GROUP BY department;

Why does it cause an error?
ABecause employee is not in the GROUP BY clause or an aggregate function.
BBecause COUNT(*) cannot be used with GROUP BY.
CBecause department is not a valid column.
DBecause GROUP BY must include all columns in SELECT.
Attempts:
2 left
💡 Hint
Check which columns must appear in GROUP BY or be aggregated.