By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Combining result sets using UNION, INTERSECT, and EXCEPT is a fundamental skill in SQL. These operations allow you to merge, filter, and compare data from multiple queries. Mastering these concepts is crucial for data analysis, reporting, and database management. Incorrect usage can lead to incomplete or incorrect data, affecting decision-making and system integrity. For instance, a financial report missing key transactions due to a misapplied UNION could result in significant errors.
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
SELECT column1, column2 FROM table1 INTERSECT SELECT column1, column2 FROM table2;
SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2;
Common Pitfall: Mismatched columns or data types will cause errors.
Use UNION to Combine Data
sql SELECT employee_id, name FROM department1 UNION SELECT employee_id, name FROM department2;
Common Pitfall: Forgetting to use UNION ALL when duplicates are needed.
Use INTERSECT to Find Common Data
sql SELECT employee_id, name FROM department1 INTERSECT SELECT employee_id, name FROM department2;
Common Pitfall: Assuming INTERSECT works like a join; it does not.
Use EXCEPT to Find Differing Data
sql SELECT employee_id, name FROM department1 EXCEPT SELECT employee_id, name FROM department2;
Experts view UNION, INTERSECT, and EXCEPT as tools for set manipulation. They understand the underlying set theory and apply these operations to solve complex data problems efficiently. Instead of memorizing syntax, they think in terms of sets and their relationships.
Exam trap: Questions that require duplicate rows in the result set.
The mistake: Assuming INTERSECT works like a join.
Exam trap: Questions that mix set operations with joins.
The mistake: Confusing EXCEPT with INTERSECT.
Exam trap: Questions that require identifying unique rows.
The mistake: Mismatched columns or data types.
Scenario: You have two tables, sales_2022 and sales_2023, with columns product_id, sales_amount.
sales_2022
sales_2023
product_id
sales_amount
Question: Combine the sales data from both years into a single result set.
Solution:1. Write the SELECT statements for each table.2. Combine them using UNION.
Answer:
SELECT product_id, sales_amount FROM sales_2022 UNION SELECT product_id, sales_amount FROM sales_2023;
Why it works: UNION combines the result sets, removing duplicates.
Scenario: You need to find products that had sales in both 2022 and 2023.
Question: Identify the common products.
Solution:1. Write the SELECT statements for each table.2. Combine them using INTERSECT.
SELECT product_id FROM sales_2022 INTERSECT SELECT product_id FROM sales_2023;
Why it works: INTERSECT returns only the common rows.
Scenario: You need to find products that had sales in 2022 but not in 2023.
Question: Identify the differing products.
Solution:1. Write the SELECT statements for each table.2. Combine them using EXCEPT.
SELECT product_id FROM sales_2022 EXCEPT SELECT product_id FROM sales_2023;
Why it works: EXCEPT returns rows from the first set not in the second.
SELECT ... UNION/INTERSECT/EXCEPT SELECT ...
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.