SQL Notes
Learn how to use the INTERSECT operator in SQL to find common rows between two result sets, with syntax, rules, real-world examples, and comparison with INNER JOIN.
Sometimes you need to find what two datasets have in common. Which customers bought from both the online store and the physical store? Which students are enrolled in both Mathematics and Computer Science? Which products appear in both the Mumbai warehouse and the Delhi warehouse?
The INTERSECT operator returns only the rows that appear in both result sets. Think of it as the overlap in a Venn diagram — only the data that exists in both queries makes it to the final result.
Finding commonality between datasets is a frequent analytical need. Marketing teams want to know which customers engage across multiple channels. HR needs to identify employees who hold multiple certifications. Product managers want to see which features are requested by both enterprise and startup customers. INTERSECT gives you a clean, declarative way to answer these intersection questions without complex JOIN conditions or correlated subqueries.
Simple Example
OnlineBuyers:
| CustomerID | Name |
|---|---|
| 1 | Rahul |
| 2 | Priya |
| 3 | Amit |
| 4 | Sneha |
StoreBuyers:
| CustomerID | Name |
|---|---|
| 2 | Priya |
| 4 | Sneha |
| 5 | Vikas |
| 6 | Neha |
SELECT CustomerID, Name FROM OnlineBuyers
INTERSECT
SELECT CustomerID, Name FROM StoreBuyers;Result:
| CustomerID | Name |
|---|---|
| 2 | Priya |
| 4 | Sneha |
Only Priya and Sneha appear in both tables, so only they appear in the result.
INTERSECT with a Single Table
You can use INTERSECT on the same table with different conditions to find rows satisfying multiple criteria:
Using INTERSECT on the same table is particularly useful when you need to verify that rows satisfy multiple independent criteria that are each complex enough to warrant their own query. While a simple AND in the WHERE clause works for basic conditions, INTERSECT becomes clearer when each condition involves its own set of JOINs, subqueries, or aggregations. It decomposes the problem into independently understandable parts.
-- Employees who are both in Engineering AND earn more than 80000
SELECT EmpID, Name FROM Employees WHERE Department = 'Engineering'
INTERSECT
SELECT EmpID, Name FROM Employees WHERE Salary > 80000;This is equivalent to:
SELECT EmpID, Name FROM Employees
WHERE Department = 'Engineering' AND Salary > 80000;While the AND version is simpler for this case, INTERSECT becomes powerful when the conditions come from different tables or complex subqueries.
How INTERSECT Determines Matches
INTERSECT compares entire rows. Two rows are considered the same only if ALL columns match:
SELECT 'Rahul', 'Engineering'
INTERSECT
SELECT 'Rahul', 'Marketing';
-- Returns NOTHING (department column differs)
SELECT 'Rahul', 'Engineering'
INTERSECT
SELECT 'Rahul', 'Engineering';
-- Returns one row (complete match)Real-World Example: Students in Multiple Courses
Find students enrolled in both Database Systems and Operating Systems:
SELECT s.StudentID, s.Name
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
JOIN Courses c ON e.CourseID = c.CourseID
WHERE c.CourseName = 'Database Systems'
INTERSECT
SELECT s.StudentID, s.Name
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
JOIN Courses c ON e.CourseID = c.CourseID
WHERE c.CourseName = 'Operating Systems';This returns only students taking both courses simultaneously.
Real-World Example: Products in Multiple Warehouses
Find products available in both the Mumbai and Bangalore warehouses:
SELECT p.ProductID, p.ProductName, p.Category
FROM Products p
JOIN MumbaiInventory mi ON p.ProductID = mi.ProductID
WHERE mi.Stock > 0
INTERSECT
SELECT p.ProductID, p.ProductName, p.Category
FROM Products p
JOIN BangaloreInventory bi ON p.ProductID = bi.ProductID
WHERE bi.Stock > 0;Real-World Example: Common Skills in Job Applicants
Find skills that multiple candidates share:
-- Skills that Candidate 101 AND Candidate 102 both have
SELECT SkillName FROM CandidateSkills WHERE CandidateID = 101
INTERSECT
SELECT SkillName FROM CandidateSkills WHERE CandidateID = 102;INTERSECT vs INNER JOIN
Both INTERSECT and INNER JOIN find common data, but they work differently:
| Feature | INTERSECT | INNER JOIN |
|---|---|---|
| Compares | Entire rows | Specific join columns |
| Duplicates | Removes them | Keeps them |
| Columns | Must be same count/type | Can differ between tables |
| Flexibility | Simple overlap | Complex matching conditions |
Example showing the difference:
-- INTERSECT: Finds identical rows (compares ALL columns)
SELECT Name, City FROM TableA
INTERSECT
SELECT Name, City FROM TableB;
-- INNER JOIN: Matches on specific condition, can return different columns
SELECT a.Name, a.City, b.Phone
FROM TableA a
INNER JOIN TableB b ON a.Name = b.Name AND a.City = b.City;Use INTERSECT when you want to find identical rows. Use INNER JOIN when you need to match on specific columns and possibly retrieve additional columns.
INTERSECT and NULL Values
Unlike regular comparisons where NULL equals nothing (not even another NULL), INTERSECT treats two NULLs as equal:
SELECT 1, NULL
INTERSECT
SELECT 1, NULL;
-- Returns one row: (1, NULL)This is an important difference from regular WHERE comparisons where NULL = NULL is UNKNOWN.
Multiple INTERSECT Operations
You can chain multiple INTERSECT operations:
-- Customers who bought from ALL three channels
SELECT CustomerID, Name FROM OnlineCustomers
INTERSECT
SELECT CustomerID, Name FROM StoreCustomers
INTERSECT
SELECT CustomerID, Name FROM PhoneCustomers;This returns only customers who appear in all three tables.
INTERSECT with ORDER BY
ORDER BY can only appear at the end:
SELECT Name FROM MumbaiEmployees
INTERSECT
SELECT Name FROM DelhiEmployees
ORDER BY Name ASC;Database Support
| Database | INTERSECT Support |
|---|---|
| PostgreSQL | Full support |
| SQL Server | Full support |
| Oracle | Full support |
| SQLite | Full support |
| MySQL | Supported from version 8.0.31 |
For older MySQL versions, you can simulate INTERSECT using INNER JOIN or EXISTS:
-- Simulating INTERSECT in older MySQL
SELECT DISTINCT a.Name, a.Email
FROM TableA a
INNER JOIN TableB b ON a.Name = b.Name AND a.Email = b.Email;Common Mistakes to Avoid
Mistake 1: Expecting partial matches. INTERSECT requires ALL columns to match. If you want to match on just some columns, use INNER JOIN.
Mistake 2: Forgetting column count must match. Both queries must return exactly the same number of columns.
Mistake 3: Using INTERSECT in MySQL 5.x. Older MySQL versions do not support INTERSECT. Use a JOIN-based alternative.
Mistake 4: Confusing with INNER JOIN behavior. INTERSECT removes duplicates automatically. INNER JOIN does not.
Best Practices
- Use INTERSECT for finding overlapping data between two result sets
- Prefer INNER JOIN when you need columns from both tables in the result
- Chain INTERSECT to find data common across three or more sources
- Check database version for MySQL compatibility
- Use meaningful aliases in complex INTERSECT queries for readability
- Consider performance — INTERSECT requires sorting/hashing to find common rows
INTERSECT is a clean, readable way to find common elements between datasets. It is particularly useful in analytical queries where you need to identify overlap between different groups, time periods, or categories.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for INTERSECT Operator in SQL.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL Complete Guide topic.
Search Terms
sql-complete-guide, sql complete guide, sql, complete, guide, set, operators, intersect
Related SQL Complete Guide Topics