Understanding Why Subqueries Are Needed in SQL
📖 Scenario: You are managing a small bookstore database. You want to find out which books have a price higher than the average price of all books in the store.
🎯 Goal: Build an SQL query using a subquery to find books priced above the average book price.
📋 What You'll Learn
Create a table called
books with columns id, title, and priceInsert exactly three books with these details: (1, 'Book A', 10), (2, 'Book B', 15), (3, 'Book C', 20)
Write a subquery to calculate the average price of all books
Use the subquery in the WHERE clause to select books with price greater than the average price
💡 Why This Matters
🌍 Real World
Subqueries help answer questions like 'Which items are above average?' or 'Which customers spent more than the average?' in real business databases.
💼 Career
Knowing how to write subqueries is essential for data analysts and database developers to perform complex data filtering and reporting.
Progress0 / 4 steps