📖 Scenario: You work at a small bookstore. You have a table that records each sale with the book title and the number of copies sold. You want to understand the total sales for each book to see which books sell the most.
🎯 Goal: Build a simple database query that uses aggregation to summarize total sales per book.
📋 What You'll Learn
Create a table called sales with columns book_title (text) and copies_sold (integer).
Insert these exact rows into sales: ('The Hobbit', 3), ('1984', 5), ('The Hobbit', 2), ('Dune', 4), ('1984', 1).
Write a query that uses GROUP BY on book_title and SUM(copies_sold) to find total copies sold per book.
Order the results by total copies sold in descending order.
💡 Why This Matters
🌍 Real World
Aggregations help businesses quickly understand total sales, averages, or counts from many records, making it easier to make decisions.
💼 Career
Database aggregation queries are essential skills for data analysts, business intelligence developers, and anyone working with data to summarize and report information.
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 rows exactly: ('The Hobbit', 3), ('1984', 5), ('The Hobbit', 2), ('Dune', 4), ('1984', 1).
MySQL
Hint
Use CREATE TABLE to make the table. Use INSERT INTO sales (book_title, copies_sold) VALUES (...) to add rows.
2
Add a variable for sorting order
Create a variable called sort_order and set it to 'DESC' to use later for ordering the results.
MySQL
Hint
Use SET @sort_order = 'DESC'; to create a variable for sorting order.
3
Write the aggregation query
Write a query that selects book_title and the sum of copies_sold as total_sold. Use GROUP BY book_title to group sales by book.
MySQL
Hint
Use SELECT book_title, SUM(copies_sold) AS total_sold FROM sales GROUP BY book_title; to summarize sales.
4
Order the results by total sales
Add an ORDER BY clause to the query to sort the results by total_sold in descending order using the variable @sort_order.
MySQL
Hint
Add ORDER BY total_sold DESC to sort books by total sales from highest to lowest.
Practice
(1/5)
1. What is the main purpose of aggregation functions like SUM and COUNT in SQL?
easy
A. To create new tables from existing ones
B. To delete duplicate rows from a table
C. To combine multiple rows into a single summary value
D. To sort rows in ascending or descending order
Solution
Step 1: Understand aggregation functions
Aggregation functions like SUM and COUNT take many rows and produce one value summarizing them.
Step 2: Identify the purpose of aggregation
They are used to combine data, not to delete, create tables, or sort rows.
Final Answer:
To combine multiple rows into a single summary value -> Option C
Quick Check:
Aggregation = summary value [OK]
Hint: Aggregation means summarizing many rows into one value [OK]
Common Mistakes:
Confusing aggregation with sorting
Thinking aggregation deletes rows
Believing aggregation creates new tables
2. Which of the following is the correct syntax to calculate the average salary from a table named employees?
easy
A. SELECT AVG(salary) FROM employees;
B. SELECT AVERAGE(salary) FROM employees;
C. SELECT MEAN(salary) FROM employees;
D. SELECT SUM(salary) / COUNT(*) FROM employees GROUP BY salary;
Solution
Step 1: Recall standard SQL aggregation functions
The correct function for average is AVG, not AVERAGE or MEAN.
Step 2: Check syntax correctness
SELECT AVG(salary) FROM employees; uses the correct AVG function with proper syntax. Options B and C use non-standard functions. SELECT SUM(salary) / COUNT(*) FROM employees GROUP BY salary; uses manual SUM/COUNT but GROUP BY salary computes per-salary value instead of overall average.
Final Answer:
SELECT AVG(salary) FROM employees; -> Option A
Quick Check:
AVG() is standard average function [OK]
Hint: Use AVG() for average, not AVERAGE or MEAN [OK]
Common Mistakes:
Using non-existent functions like AVERAGE or MEAN
Trying to calculate average manually instead of using AVG()
Missing semicolon at the end
3. Given the table sales with columns region and amount, what will this 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. Average sales amount for all regions combined
D. Number of sales records in the table
Solution
Step 1: Understand GROUP BY with SUM
The query groups rows by region and sums the amount in each group.
Step 2: Interpret the result
It returns one row per region with the total sales amount for that region.
Final Answer:
Total sales amount for each region -> Option A
Quick Check:
GROUP BY region + SUM(amount) = total per region [OK]
Hint: GROUP BY groups rows; SUM adds values per group [OK]
Common Mistakes:
Thinking it returns all individual sales
Confusing SUM with AVG
Ignoring GROUP BY effect
4. Identify the error in this query:
SELECT department, COUNT(employee_id) FROM employees;
medium
A. COUNT cannot be used with employee_id
B. Missing GROUP BY clause for department
C. SELECT must include WHERE clause
D. employee_id should be inside SUM()
Solution
Step 1: Check aggregation with non-aggregated column
The query selects department (non-aggregated) and COUNT(employee_id) (aggregated) without GROUP BY.
Step 2: Understand SQL rules for aggregation
When mixing aggregated and non-aggregated columns, GROUP BY is required on non-aggregated columns.
Final Answer:
Missing GROUP BY clause for department -> Option B
Quick Check:
Non-aggregated columns need GROUP BY [OK]
Hint: Use GROUP BY when selecting non-aggregated columns with aggregation [OK]
Common Mistakes:
Omitting GROUP BY with mixed columns
Misusing COUNT on non-numeric columns
Adding unnecessary WHERE clause
5. You want to find the department with the highest average salary from the employees table. Which query correctly summarizes this data?
hard
A. SELECT department, MAX(AVG(salary)) FROM employees GROUP BY department;
B. SELECT department, SUM(salary) FROM employees;
C. SELECT MAX(salary) FROM employees GROUP BY department;
D. SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1;
Solution
Step 1: Understand the goal
We want the department with the highest average salary, so we need average per department and then pick the top one.
Step 2: Analyze each option
SELECT department, MAX(AVG(salary)) FROM employees GROUP BY department; is invalid because MAX(AVG(salary)) is not allowed directly. SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1; correctly groups by department, calculates average salary, orders descending, and limits to one result. SELECT MAX(salary) FROM employees GROUP BY department; finds max salary per department, not average. SELECT department, SUM(salary) FROM employees; sums salary but does not group or find max.
Final Answer:
SELECT department, AVG(salary) FROM employees GROUP BY department ORDER BY AVG(salary) DESC LIMIT 1; -> Option D
Quick Check:
Group + AVG + ORDER BY + LIMIT = highest average [OK]
Hint: Use GROUP BY, AVG, ORDER BY DESC, LIMIT 1 for top average [OK]
Common Mistakes:
Trying to nest aggregation functions incorrectly
Not ordering results to find highest value
Missing GROUP BY when aggregating
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.