JOIN with aggregate functions
📖 Scenario: You are managing a small online bookstore database. You have two tables: books and sales. The books table stores book details, and the sales table records each sale of a book. You want to find out how many copies of each book have been sold in total.
🎯 Goal: Create a SQL query that joins the books and sales tables, then uses an aggregate function to calculate the total number of copies sold for each book.
📋 What You'll Learn
Create a
books table with columns book_id (integer), title (string), and author (string).Create a
sales table with columns sale_id (integer), book_id (integer), and quantity (integer).Write a SQL query that joins
books and sales on book_id.Use the
SUM() aggregate function to calculate total copies sold per book.Group the results by
book_id and title.💡 Why This Matters
🌍 Real World
Bookstores and many businesses use SQL joins and aggregate functions to analyze sales data and understand product performance.
💼 Career
Database analysts and developers often write queries combining joins and aggregates to generate reports and insights for decision-making.
Progress0 / 4 steps