0
0
SQLquery~30 mins

Why grouping is needed in SQL - See It in Action

Choose your learning style9 modes available
Understanding Why Grouping is Needed in SQL
📖 Scenario: You work at a small bookstore. You have a list of sales records showing which book was sold and how many copies. You want to find out how many copies of each book were sold in total.
🎯 Goal: Build a SQL 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 and copies_sold
Insert the exact sales data provided
Write a SQL query that groups the sales by book_title
Use SUM() to calculate total copies sold per book
💡 Why This Matters
🌍 Real World
Grouping data is common in sales reports, inventory summaries, and any situation where you want totals or averages per category.
💼 Career
Understanding GROUP BY is essential for data analysis, reporting, and writing effective SQL queries in many jobs.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns book_title (text) and copies_sold (integer). Insert these exact rows: ('The Hobbit', 3), ('1984', 5), ('The Hobbit', 2), ('Dune', 4), ('1984', 1).
SQL
Need a hint?

Use CREATE TABLE to make the table and INSERT INTO to add the rows.

2
Add a query to group sales by book
Write a SQL query that selects book_title and the total copies sold for each book. Use GROUP BY book_title to group the rows by book title.
SQL
Need a hint?

Use SUM(copies_sold) to add copies sold per book and GROUP BY book_title to group rows.

3
Explain why grouping is needed
Add a comment explaining why GROUP BY is needed in this query to get total copies sold per book.
SQL
Need a hint?

Think about how grouping helps add up sales for each book instead of all sales together.

4
Complete the query with ordering
Add ORDER BY total_copies DESC to the query to show books with the highest total copies sold first.
SQL
Need a hint?

Use ORDER BY total_copies DESC to sort results from highest to lowest total copies sold.