Using UNION ALL to Combine Tables with Duplicates
📖 Scenario: You work at a bookstore that has two separate tables for new arrivals and best sellers. Some books appear in both tables because they are both new and popular.You want to create a combined list of all books from both tables, including duplicates, to analyze total stock.
🎯 Goal: Build a SQL query that uses UNION ALL to combine the new_arrivals and best_sellers tables, keeping duplicate book entries.
📋 What You'll Learn
Create a table called
new_arrivals with columns book_id and title and insert 3 specific books.Create a table called
best_sellers with columns book_id and title and insert 3 specific books, including one that duplicates a book from new_arrivals.Write a SQL query using
UNION ALL to combine all rows from both tables, including duplicates.Ensure the final query selects
book_id and title from the combined result.💡 Why This Matters
🌍 Real World
Combining data from multiple sources like sales and inventory lists to get a full picture including duplicates.
💼 Career
Understanding <code>UNION ALL</code> is important for data analysts and database developers who merge datasets without losing repeated entries.
Progress0 / 4 steps