Using UNION and UNION ALL in PostgreSQL
📖 Scenario: You work at a bookstore that has two separate tables: one for new arrivals and one for best sellers. You want to create combined lists of books for different purposes.
🎯 Goal: Build SQL queries using UNION and UNION ALL to combine data from two tables: new_arrivals and best_sellers. You will first create the tables, then write queries to combine their data without duplicates and with duplicates.
📋 What You'll Learn
Create two tables named
new_arrivals and best_sellers with columns book_id and title.Insert specific book records into both tables.
Write a query using
UNION to combine the two tables without duplicate titles.Write a query using
UNION ALL to combine the two tables including duplicates.💡 Why This Matters
🌍 Real World
Combining data from multiple sources is common in real-world databases, such as merging product lists, customer records, or event logs.
💼 Career
Understanding how to use UNION and UNION ALL is essential for database querying and reporting tasks in roles like data analyst, database developer, and backend engineer.
Progress0 / 4 steps