SQL Set Operators Examples
Quick Answer
SQL Set Operators like UNION, INTERSECT, and EXCEPT allow combining results from multiple SELECT queries. They help merge, find common, or exclude data sets efficiently. Understanding these operators with examples is essential for effective SQL querying.
Learning Objectives
- Understand how to use SQL set operators to combine query results.
- Learn the differences between UNION, INTERSECT, and EXCEPT with examples.
- Apply set operators to solve practical data retrieval problems.
Introduction
SQL set operators are powerful tools to combine results from multiple SELECT statements.
They help you merge, compare, or exclude data sets in a straightforward way.
This tutorial focuses on practical examples of UNION, INTERSECT, and EXCEPT to solidify your understanding.
Set operations in SQL enable flexible and efficient data retrieval.
UNION Operator Example
The UNION operator combines the results of two SELECT queries and removes duplicate rows.
Both queries must return the same number of columns with compatible data types.
- Combines results vertically.
- Removes duplicates by default.
- Use UNION ALL to include duplicates.
Example Query
Suppose we have two tables: Employees_US and Employees_UK, each listing employee names.
INTERSECT Operator Example
The INTERSECT operator returns only the rows that appear in both SELECT query results.
It is useful to find common data between two datasets.
- Returns common rows only.
- Both queries must have matching columns and data types.
EXCEPT Operator Example
The EXCEPT operator returns rows from the first query that do not appear in the second query.
It helps to find differences between datasets.
- Returns rows unique to the first query.
- Both queries must have the same number of columns and compatible types.
Practical Example
This query lists all unique employee names from both US and UK employee tables.
This query returns employee names that exist in both US and UK employee tables.
This query lists employee names that are in the US table but not in the UK table.
Examples
SELECT name FROM Employees_US
UNION
SELECT name FROM Employees_UK;This query lists all unique employee names from both US and UK employee tables.
SELECT name FROM Employees_US
INTERSECT
SELECT name FROM Employees_UK;This query returns employee names that exist in both US and UK employee tables.
SELECT name FROM Employees_US
EXCEPT
SELECT name FROM Employees_UK;This query lists employee names that are in the US table but not in the UK table.
Best Practices
- Ensure both SELECT statements have the same number of columns and compatible data types.
- Use UNION ALL if you want to include duplicates.
- Test queries separately before combining with set operators.
- Use aliases to clarify column names when needed.
Common Mistakes
- Mismatching column counts between queries.
- Using incompatible data types in corresponding columns.
- Expecting UNION to preserve duplicates without UNION ALL.
- Forgetting that set operators remove duplicates by default.
Hands-on Exercise
Combine Customer Lists
Write a query to combine customer names from two tables, Customers_A and Customers_B, removing duplicates.
Expected output: A list of unique customer names from both tables.
Hint: Use the UNION operator.
Find Common Products
Write a query to find product IDs that appear in both Products_Store1 and Products_Store2 tables.
Expected output: A list of product IDs common to both stores.
Hint: Use the INTERSECT operator.
Identify Exclusive Orders
Write a query to find orders present in Orders_2023 but not in Orders_2022.
Expected output: A list of orders unique to 2023.
Hint: Use the EXCEPT operator.
Interview Questions
What is the difference between UNION and UNION ALL?
InterviewUNION removes duplicate rows from the combined result set, while UNION ALL includes all rows, including duplicates.
Can you use set operators with different numbers of columns in the SELECT statements?
InterviewNo, all SELECT statements combined with set operators must have the same number of columns with compatible data types.
When would you use the EXCEPT operator?
InterviewEXCEPT is used to find rows present in the first query but not in the second, useful for identifying differences between datasets.
MCQ Quiz
1. What is the best first step when learning Examples?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Examples?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. SQL Set Operators like UNION, INTERSECT, and EXCEPT allow combining results from multiple SELECT queries.
B. Examples never needs examples
C. Examples is unrelated to practical work
D. Examples should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- UNION combines results from two queries and removes duplicates.
- INTERSECT returns only rows common to both queries.
- EXCEPT returns rows from the first query that are not in the second.
- Set operators require queries to have the same number of columns and compatible data types.
- SQL Set Operators like UNION, INTERSECT, and EXCEPT allow combining results from multiple SELECT queries.
Summary
SQL set operators allow combining and comparing results from multiple SELECT queries.
UNION merges results and removes duplicates, INTERSECT finds common rows, and EXCEPT identifies differences.
Using these operators effectively can simplify complex data retrieval tasks.
Frequently Asked Questions
Do set operators work with different column names?
Yes, but the number and data types of columns must match. Column names do not have to be the same.
What happens if the data types do not match in set operators?
The query will fail with a type mismatch error. Data types must be compatible for set operators to work.
Can set operators be chained together?
Yes, you can chain multiple set operators in a single query, but be mindful of operator precedence and parentheses.





