Subquery vs JOIN Performance Trade-off
📖 Scenario: You are working as a database analyst for an online bookstore. You have two tables: books and sales. The books table contains book details, and the sales table records each sale with the book's ID and quantity sold.Your task is to find the total quantity sold for each book using two different SQL approaches: a subquery and a JOIN. This will help you understand the performance trade-offs between these methods.
🎯 Goal: Build two SQL queries to calculate total sales per book: one using a subquery and one using a JOIN. Compare their structure and understand when each is useful.
📋 What You'll Learn
Create a
books table with columns book_id (integer) and title (text).Create a
sales table with columns sale_id (integer), book_id (integer), and quantity (integer).Write a query using a subquery to get each book's title and total quantity sold.
Write a query using a JOIN to get each book's title and total quantity sold.
💡 Why This Matters
🌍 Real World
This project simulates a common task in business analytics: summarizing sales data by product using different SQL techniques.
💼 Career
Understanding subqueries and JOINs is essential for database developers, analysts, and anyone working with relational databases to write efficient and clear queries.
Progress0 / 4 steps