Fatskills
Practice. Master. Repeat.
Study Guide: Database-Systems: Set-Operations - UNION, INTERSECT, EXCEPT, Combining Result Sets
Source: https://www.fatskills.com/cset/chapter/database-systems-set-operations-union-intersect-except-combining-result-sets

Database-Systems: Set-Operations - UNION, INTERSECT, EXCEPT, Combining Result Sets

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is and Why It Matters

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.

Core Knowledge (What You Must Internalize)

  • UNION: Combines the result sets of two or more SELECT statements. (Why this matters: Allows you to merge data from different queries into a single result set.)
  • INTERSECT: Returns the common rows between two SELECT statements. (Why this matters: Useful for finding overlapping data between two datasets.)
  • EXCEPT: Returns the rows from the first SELECT statement that are not present in the second SELECT statement. (Why this matters: Helps in identifying differences between two datasets.)
  • Key Distinctions: UNION includes all rows, INTERSECT includes only common rows, and EXCEPT includes only differing rows.
  • Typical Units: Result sets must have the same number of columns with compatible data types.

Step?by?Step Deep Dive

  1. Understand the Basic Syntax
  2. UNION: SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
  3. INTERSECT: SELECT column1, column2 FROM table1 INTERSECT SELECT column1, column2 FROM table2;
  4. EXCEPT: SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2;
  5. Underlying Principle: These operations work on the principle of set theory, treating result sets as sets of rows.
  6. Example: Combining employee lists from two departments.
  7. Common Pitfall: Mismatched columns or data types will cause errors.

  8. Use UNION to Combine Data

  9. Action: Write two SELECT statements and combine them with UNION.
  10. Principle: UNION removes duplicate rows unless UNION ALL is used.
  11. Example: sql SELECT employee_id, name FROM department1 UNION SELECT employee_id, name FROM department2;
  12. Common Pitfall: Forgetting to use UNION ALL when duplicates are needed.

  13. Use INTERSECT to Find Common Data

  14. Action: Write two SELECT statements and combine them with INTERSECT.
  15. Principle: INTERSECT returns only the rows that appear in both result sets.
  16. Example: sql SELECT employee_id, name FROM department1 INTERSECT SELECT employee_id, name FROM department2;
  17. Common Pitfall: Assuming INTERSECT works like a join; it does not.

  18. Use EXCEPT to Find Differing Data

  19. Action: Write two SELECT statements and combine them with EXCEPT.
  20. Principle: EXCEPT returns rows from the first result set that are not in the second.
  21. Example: sql SELECT employee_id, name FROM department1 EXCEPT SELECT employee_id, name FROM department2;
  22. Common Pitfall: Confusing EXCEPT with INTERSECT; they serve different purposes.

How Experts Think About This Topic

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.

Common Mistakes (Even Smart People Make)

  1. The mistake: Using UNION without ALL when duplicates are needed.
  2. Why it's wrong: UNION removes duplicates by default.
  3. How to avoid: Use UNION ALL to include duplicates.
  4. Exam trap: Questions that require duplicate rows in the result set.

  5. The mistake: Assuming INTERSECT works like a join.

  6. Why it's wrong: INTERSECT finds common rows, not related rows.
  7. How to avoid: Understand the difference between set operations and joins.
  8. Exam trap: Questions that mix set operations with joins.

  9. The mistake: Confusing EXCEPT with INTERSECT.

  10. Why it's wrong: EXCEPT finds differences, not commonalities.
  11. How to avoid: Remember EXCEPT is for differences, INTERSECT for commonalities.
  12. Exam trap: Questions that require identifying unique rows.

  13. The mistake: Mismatched columns or data types.

  14. Why it's wrong: Set operations require matching columns and data types.
  15. How to avoid: Verify column names and data types before applying set operations.
  16. Exam trap: Questions with mismatched columns or data types.

Practice with Real Scenarios

Scenario: You have two tables, sales_2022 and sales_2023, with columns 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.

Answer:

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.

Answer:

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.

Quick Reference Card

  • Core Rule: Use UNION for combining, INTERSECT for commonalities, and EXCEPT for differences.
  • Key Formula: SELECT ... UNION/INTERSECT/EXCEPT SELECT ...
  • Critical Facts:
  • UNION removes duplicates.
  • INTERSECT finds common rows.
  • EXCEPT finds differing rows.
  • Dangerous Pitfall: Mismatched columns or data types.
  • Mnemonic: Unites, Intersects, Excludes.

If You're Stuck (Exam or Real Life)

  • What to check first: Verify column names and data types.
  • How to reason from first principles: Think in terms of sets and their operations.
  • When to use estimation: Estimate the number of rows to confirm the operation's correctness.
  • Where to find the answer: Refer to SQL documentation or practice with sample datasets.

Related Topics

  • Joins: Understand how joins differ from set operations.
  • Subqueries: Learn how to use subqueries for more complex data manipulation.