SQL Set Operators: Understanding INTERSECT
Quick Answer
The SQL INTERSECT operator returns the common rows from two SELECT queries. It helps find records that appear in both result sets, eliminating duplicates. INTERSECT is useful for comparing datasets and ensuring data consistency.
Learning Objectives
- Understand what the SQL INTERSECT operator does and when to use it.
- Write SQL queries using INTERSECT to find common records between datasets.
- Differentiate INTERSECT from other set operators like UNION and EXCEPT.
Introduction
SQL set operators allow you to combine results from multiple SELECT queries in meaningful ways.
INTERSECT is one such operator that returns only the rows present in both query results, helping identify common data points.
This tutorial explains how to use INTERSECT effectively with examples and best practices.
Set theory in SQL helps us find commonalities and differences in data.
What is the INTERSECT Operator?
The INTERSECT operator returns the intersection of two SELECT query results.
It outputs only the rows that appear in both result sets, removing duplicates automatically.
- Both queries must have the same number of columns.
- Corresponding columns must have compatible data types.
- The order of columns matters for matching rows.
Syntax and Usage
The basic syntax of INTERSECT is straightforward:
You write two SELECT statements separated by the INTERSECT keyword.
| SQL Statement |
|---|
| SELECT column_list FROM table1 |
| INTERSECT |
| SELECT column_list FROM table2; |
Example: Finding Common Customers
Suppose you have two tables: Customers_US and Customers_EU, each listing customers in different regions.
To find customers present in both regions, you can use INTERSECT.
SQL Query Example
This query returns customers who appear in both tables.
Practical Example
This query returns the customers who exist in both the US and EU customer tables.
Examples
SELECT CustomerID, CustomerName FROM Customers_US
INTERSECT
SELECT CustomerID, CustomerName FROM Customers_EU;This query returns the customers who exist in both the US and EU customer tables.
Best Practices
- Ensure both SELECT statements have the same number of columns with compatible data types.
- Use INTERSECT when you need to find common records between two datasets.
- Avoid using ORDER BY inside individual SELECT statements when using INTERSECT; apply it after the final result if needed.
- Test queries with sample data to confirm expected results.
Common Mistakes
- Mismatching the number of columns in the SELECT statements.
- Using incompatible data types between corresponding columns.
- Expecting INTERSECT to return duplicates; it always removes duplicates.
- Confusing INTERSECT with JOIN operations.
Hands-on Exercise
Find Common Products
Write a query using INTERSECT to find products listed in both the Products_StoreA and Products_StoreB tables.
Expected output: A list of products available in both stores without duplicates.
Hint: Select the same columns from both tables and use INTERSECT between the SELECT statements.
Compare Employee Lists
Use INTERSECT to find employees who work in both the Sales and Marketing departments.
Expected output: Employees common to both departments.
Hint: Select employee IDs and names from both department tables and intersect the results.
Interview Questions
What does the SQL INTERSECT operator do?
InterviewINTERSECT returns the rows that are common to the results of two SELECT queries, removing duplicates.
How is INTERSECT different from UNION?
InterviewINTERSECT returns only common rows between queries, while UNION returns all unique rows from both queries combined.
Can INTERSECT be used with different numbers of columns in SELECT statements?
InterviewNo, both SELECT statements must have the same number of columns with compatible data types for INTERSECT to work.
MCQ Quiz
1. What is the best first step when learning INTERSECT?
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 INTERSECT?
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. The SQL INTERSECT operator returns the common rows from two SELECT queries.
B. INTERSECT never needs examples
C. INTERSECT is unrelated to practical work
D. INTERSECT should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- INTERSECT returns only rows common to both SELECT statements.
- Duplicates are automatically removed in INTERSECT results.
- All SELECT statements in INTERSECT must have the same number of columns and compatible data types.
- INTERSECT is useful for comparing datasets and validating data consistency.
- The SQL INTERSECT operator returns the common rows from two SELECT queries.
Summary
The SQL INTERSECT operator is a powerful tool to find common records between two query results.
It requires matching column counts and compatible data types and automatically removes duplicates.
Understanding INTERSECT helps you compare datasets and validate data efficiently.
Frequently Asked Questions
Can INTERSECT be used with more than two SELECT statements?
Yes, you can chain multiple INTERSECT operations to find common rows across several queries.
Does INTERSECT preserve the order of rows?
No, INTERSECT does not guarantee row order. Use ORDER BY after the final INTERSECT query to sort results.
Is INTERSECT supported in all SQL databases?
Most modern SQL databases support INTERSECT, but some older or limited systems may not. Check your database documentation.





