Combining Multiple Aggregates in SQL
📖 Scenario: You are managing a small bookstore's sales database. You want to analyze the sales data to understand the total number of books sold and the average price of books sold.
🎯 Goal: Build an SQL query that combines multiple aggregate functions to find the total quantity of books sold and the average sale price from the sales table.
📋 What You'll Learn
Create a table called
sales with columns book_id (integer), quantity (integer), and price (decimal).Insert the exact rows: (1, 3, 15.00), (2, 2, 20.00), (3, 5, 10.00).
Write a query that uses
SUM(quantity) and AVG(price) in the SELECT clause.Name the output columns
total_books_sold and average_price respectively.💡 Why This Matters
🌍 Real World
Bookstores and many businesses use aggregate queries to summarize sales data quickly.
💼 Career
Knowing how to combine multiple aggregates is essential for data analysis and reporting roles.
Progress0 / 4 steps