📖 Scenario: You work for a small bookstore that wants to analyze sales data.The store keeps a record of each sale with the book title and the number of copies sold.
🎯 Goal: Create a MySQL query that groups sales by book title and calculates the total copies sold for each book.
📋 What You'll Learn
Create a table called sales with columns book_title (VARCHAR) and copies_sold (INT).
Insert the exact sales data given.
Write a query that uses GROUP BY book_title to sum copies_sold for each book.
Order the results by total copies sold in descending order.
💡 Why This Matters
🌍 Real World
Grouping and summarizing sales data helps businesses understand which products sell best.
💼 Career
Database analysts and developers use GROUP BY queries to generate reports and insights from data.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns book_title as VARCHAR(100) and copies_sold as INT. Then insert these exact rows: ('The Alchemist', 5), ('The Alchemist', 3), ('1984', 7), ('1984', 2), ('Brave New World', 4).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Add a variable for ordering direction
Create a variable called order_direction and set it to 'DESC' to specify descending order for the query results.
MySQL
Hint
Use SET @order_direction = 'DESC'; to create a user variable in MySQL.
3
Write the GROUP BY query to sum copies sold
Write a SELECT query that selects book_title and the sum of copies_sold as total_copies from sales. Use GROUP BY book_title to group the results by book title.
MySQL
Hint
Use SUM(copies_sold) to add up copies sold per book.
4
Order the grouped results by total copies sold
Add an ORDER BY clause to the previous query to order the results by total_copies using the variable @order_direction for the direction.
MySQL
Hint
Use ORDER BY total_copies DESC to sort from highest to lowest.
Practice
(1/5)
1. What is the main purpose of the GROUP BY clause in a MySQL query?
easy
A. To filter rows based on a condition
B. To sort the result set in ascending order
C. To join two or more tables together
D. To group rows that have the same values in specified columns
Solution
Step 1: Understand the role of GROUP BY
The GROUP BY clause groups rows that share the same values in one or more columns.
Step 2: Differentiate from other clauses
Sorting is done by ORDER BY, filtering by WHERE, and joining by JOIN. GROUP BY is specifically for grouping rows.
Final Answer:
To group rows that have the same values in specified columns -> Option D
Quick Check:
GROUP BY = group rows by column values [OK]
Hint: GROUP BY groups rows by column values [OK]
Common Mistakes:
Confusing GROUP BY with ORDER BY
Using GROUP BY to filter rows
Thinking GROUP BY joins tables
2. Which of the following is the correct syntax to group rows by the column department in a MySQL query?
easy
A. SELECT * FROM employees GROUP BY department;
B. SELECT * FROM employees GROUP department BY;
C. SELECT * FROM employees ORDER BY department GROUP;
D. SELECT * FROM employees WHERE department GROUP BY;
Solution
Step 1: Recall correct GROUP BY syntax
The correct syntax is: SELECT columns FROM table GROUP BY column_name;
Step 2: Check each option
SELECT * FROM employees GROUP BY department; matches the correct syntax exactly. Others have misplaced keywords or wrong order.
Final Answer:
SELECT * FROM employees GROUP BY department; -> Option A
Quick Check:
GROUP BY column_name syntax = SELECT * FROM employees GROUP BY department; [OK]
Hint: GROUP BY follows SELECT ... FROM ... GROUP BY column [OK]
Common Mistakes:
Placing GROUP BY after WHERE clause incorrectly
Swapping GROUP BY and ORDER BY keywords
Using GROUP BY without specifying columns
3. Given the table sales with columns region and amount, what will the following query return?
SELECT region, SUM(amount) FROM sales GROUP BY region;
medium
A. Total sales amount for each region
B. List of all sales amounts without grouping
C. Syntax error due to missing alias
D. Average sales amount for each region
Solution
Step 1: Understand the query components
The query groups rows by region and sums the amount for each group.
Step 2: Interpret the result
It returns one row per region with the total sales amount summed up.
Final Answer:
Total sales amount for each region -> Option A
Quick Check:
GROUP BY + SUM = total per group [OK]
Hint: SUM with GROUP BY gives total per group [OK]
Common Mistakes:
Thinking it returns average instead of sum
Expecting all rows without grouping
Assuming syntax error without alias
4. Identify the error in this query:
SELECT department, COUNT(employee_id) FROM employees WHERE salary > 50000 GROUP BY employee_id;
medium
A. COUNT cannot be used with employee_id
B. GROUP BY should use department, not employee_id
C. WHERE clause cannot be used with GROUP BY
D. Missing ORDER BY clause
Solution
Step 1: Analyze GROUP BY usage
The query groups by employee_id but selects department, which causes mismatch.
Step 2: Correct grouping column
To count employees per department, GROUP BY should be on department, matching the selected column.
Final Answer:
GROUP BY should use department, not employee_id -> Option B
Quick Check:
GROUP BY column must match selected non-aggregated columns [OK]
Hint: GROUP BY columns must match selected non-aggregated columns [OK]
Common Mistakes:
Grouping by wrong column not in SELECT
Thinking WHERE conflicts with GROUP BY
Assuming ORDER BY is mandatory
5. You have a products table with columns category, price, and stock. Which query correctly finds the average price and total stock for each category?
hard
A. SELECT category, AVG(price), SUM(stock) FROM products GROUP BY price;
B. SELECT category, price, stock FROM products GROUP BY category;
C. SELECT category, AVG(price), SUM(stock) FROM products GROUP BY category;
D. SELECT category, AVG(price), SUM(stock) FROM products;
Solution
Step 1: Identify aggregation needs
We want average price and total stock per category, so use AVG(price) and SUM(stock).
Step 2: Group by category
To get results per category, GROUP BY category is required.
Step 3: Check each option
SELECT category, AVG(price), SUM(stock) FROM products GROUP BY category; correctly uses GROUP BY category with AVG and SUM. Others miss grouping or group by wrong column.
Final Answer:
SELECT category, AVG(price), SUM(stock) FROM products GROUP BY category; -> Option C
Quick Check:
Aggregate functions + GROUP BY category = SELECT category, AVG(price), SUM(stock) FROM products GROUP BY category; [OK]
Hint: Use GROUP BY category with AVG and SUM for correct aggregation [OK]
Common Mistakes:
Grouping by wrong column like price
Not using GROUP BY with aggregate functions
Selecting columns without aggregation or grouping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.