LEFT JOIN preserving all left rows
📖 Scenario: You work for a small bookstore that keeps two tables: one for books and one for sales. Some books may not have any sales yet. You want to create a report that shows all books, including those without sales, so the store can see which books have not sold yet.
🎯 Goal: Build a SQL query using LEFT JOIN to list all books with their sales information if available, preserving all books even if they have no sales.
📋 What You'll Learn
Create a table called
books with columns book_id (integer) and title (text).Create a table called
sales with columns sale_id (integer), book_id (integer), and quantity (integer).Insert the exact data into
books: (1, 'The Great Gatsby'), (2, '1984'), (3, 'To Kill a Mockingbird').Insert the exact data into
sales: (101, 1, 3), (102, 1, 2), (103, 3, 5).Write a
LEFT JOIN query to select all books and their sales quantities, showing NULL for sales if none exist.💡 Why This Matters
🌍 Real World
Bookstores and many businesses use LEFT JOIN to create reports that include all items, even those without related records like sales or orders.
💼 Career
Understanding LEFT JOIN is essential for data analysts and database developers to write queries that show complete data sets including missing or unmatched records.
Progress0 / 4 steps