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
GROUP BY Multiple Columns in SQL
📖 Scenario: You work for a small bookstore that wants to analyze its sales data. The store keeps records of each sale, including the book title, the genre, and the number of copies sold. Your manager wants to see how many copies were sold for each combination of book title and genre.
🎯 Goal: Create an SQL query that groups sales data by both book_title and genre columns, and calculates the total copies sold for each group.
📋 What You'll Learn
Create a table called sales with columns book_title (text), genre (text), and copies_sold (integer).
Insert the exact sales data provided into the sales table.
Write a query that groups the data by book_title and genre.
Calculate the sum of copies_sold for each group.
💡 Why This Matters
🌍 Real World
Grouping sales data by multiple columns helps businesses understand combined effects, like how many copies of each book in each genre sold.
💼 Career
SQL GROUP BY with multiple columns is a common skill for data analysts and database developers to summarize and report data effectively.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns book_title (text), genre (text), and copies_sold (integer). Then insert these exact rows into sales: ('The Alchemist', 'Fiction', 5), ('The Alchemist', 'Adventure', 3), ('Deep Work', 'Self-help', 7), ('Deep Work', 'Productivity', 4), ('The Alchemist', 'Fiction', 2).
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Set up the SELECT statement with columns
Write a SELECT statement that selects book_title and genre from the sales table. Do not add GROUP BY or aggregation yet.
SQL
Hint
Use SELECT book_title, genre FROM sales to see the columns.
3
Add GROUP BY for book_title and genre
Modify the SELECT statement to group the results by both book_title and genre using GROUP BY book_title, genre.
SQL
Hint
Use GROUP BY book_title, genre to group by both columns.
4
Calculate total copies sold per group
Add a column to the SELECT statement that calculates the sum of copies_sold for each group. Use SUM(copies_sold) AS total_copies and keep the GROUP BY book_title, genre.
SQL
Hint
Use SUM(copies_sold) AS total_copies in SELECT and keep GROUP BY book_title, genre.
Practice
(1/5)
1. What does the SQL clause GROUP BY column1, column2 do?
easy
A. Sorts the table by column1 and then column2
B. Groups rows by unique combinations of values in column1 and column2
C. Filters rows where column1 equals column2
D. Joins two tables on column1 and column2
Solution
Step 1: Understand GROUP BY purpose
The GROUP BY clause groups rows that have the same values in specified columns.
Step 2: Apply to multiple columns
When multiple columns are listed, grouping happens on unique combinations of those columns' values.
Final Answer:
Groups rows by unique combinations of values in column1 and column2 -> Option B
Quick Check:
GROUP BY multiple columns = group by combinations [OK]
Hint: GROUP BY multiple columns groups by combined unique values [OK]
Common Mistakes:
Thinking GROUP BY sorts data
Confusing GROUP BY with WHERE filtering
Assuming GROUP BY joins tables
2. Which of the following is the correct syntax to group data by two columns named city and year?
easy
A. SELECT city, year FROM table GROUP city, year;
B. SELECT city, year FROM table ORDER BY city, year;
C. SELECT city, year FROM table GROUP BY city, year;
D. SELECT city, year FROM table GROUP BY city year;
Solution
Step 1: Recall GROUP BY syntax
The correct syntax is GROUP BY followed by column names separated by commas.
Step 2: Check each option
SELECT city, year FROM table GROUP BY city, year; uses correct syntax with commas. SELECT city, year FROM table ORDER BY city, year; uses ORDER BY, which is for sorting. SELECT city, year FROM table GROUP city, year; misses BY keyword. SELECT city, year FROM table GROUP BY city year; misses comma between columns.
Final Answer:
SELECT city, year FROM table GROUP BY city, year; -> Option C
Quick Check:
GROUP BY columns separated by commas [OK]
Hint: Use GROUP BY with commas between columns [OK]
Common Mistakes:
Using ORDER BY instead of GROUP BY
Omitting BY keyword after GROUP
Missing commas between column names
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. Total sales amount for each region only
B. Syntax error due to missing GROUP BY columns
C. Total sales amount for each product only
D. Total sales amount for each region and product combination
Solution
Step 1: Analyze SELECT and GROUP BY columns
The query groups rows by both region and product, so each group is a unique pair of region and product.
Step 2: Understand aggregation function SUM(amount)
SUM(amount) calculates total sales amount for each group of region and product.
Final Answer:
Total sales amount for each region and product combination -> Option D
Quick Check:
GROUP BY region, product + SUM = totals per pair [OK]
Hint: GROUP BY columns + SUM aggregates per group [OK]
Common Mistakes:
Thinking it sums only by one column
Assuming syntax error without reason
Ignoring that all selected non-aggregated columns must be grouped
4. Identify the error in this query:
SELECT department, role, COUNT(*) FROM employees GROUP BY department;
medium
A. Missing role column in GROUP BY clause
B. COUNT(*) cannot be used with GROUP BY
C. department should not be in GROUP BY
D. SELECT must include only aggregated columns
Solution
Step 1: Check SELECT columns vs GROUP BY columns
Columns in SELECT that are not aggregated must appear in GROUP BY. Here, role is in SELECT but missing in GROUP BY.
Step 2: Understand aggregation rules
COUNT(*) is valid, but all non-aggregated columns must be grouped to avoid errors.
Final Answer:
Missing role column in GROUP BY clause -> Option A
Quick Check:
All non-aggregated SELECT columns must be in GROUP BY [OK]
Hint: Include all non-aggregated SELECT columns in GROUP BY [OK]
Common Mistakes:
Ignoring missing columns in GROUP BY
Thinking COUNT(*) is invalid with GROUP BY
Assuming GROUP BY only needs one column
5. You have a transactions table with columns customer_id, month, and amount. You want to find the average transaction amount per customer per month, but only for months where the customer made more than 3 transactions. Which query correctly achieves this?
hard
A. SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY customer_id, month HAVING COUNT(*) > 3;
B. SELECT customer_id, month, AVG(amount) FROM transactions WHERE COUNT(*) > 3 GROUP BY customer_id, month;
C. SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY customer_id HAVING COUNT(*) > 3;
D. SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY month HAVING COUNT(*) > 3;
Solution
Step 1: Understand filtering groups with HAVING
HAVING filters groups after grouping. To filter groups with more than 3 transactions, use HAVING COUNT(*) > 3.
Step 2: Group by both customer_id and month
To get average per customer per month, group by both columns.
Step 3: Check each option
SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY customer_id, month HAVING COUNT(*) > 3; correctly uses GROUP BY customer_id, month and HAVING COUNT(*) > 3. SELECT customer_id, month, AVG(amount) FROM transactions WHERE COUNT(*) > 3 GROUP BY customer_id, month; misuses WHERE with COUNT(). SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY customer_id HAVING COUNT(*) > 3; groups only by customer_id, missing month. SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY month HAVING COUNT(*) > 3; groups only by month, missing customer_id.
Final Answer:
SELECT customer_id, month, AVG(amount) FROM transactions GROUP BY customer_id, month HAVING COUNT(*) > 3; -> Option A
Quick Check:
Use HAVING to filter grouped counts [OK]
Hint: Use HAVING to filter groups after GROUP BY [OK]