Diagram illustrating SQL INTERSECT operation between two result sets

Understanding SQL INTERSECT: Syntax, Use Cases, and Examples

When you need to find rows that exist in the results of two separate SQL queries, the SQL INTERSECT operator delivers a clean solution. INTERSECT returns only the common rows between both result sets, filtering out any that are present in one but not the other. This makes it especially useful for tasks that require identifying overlap or commonality within your database data. In this post, you’ll learn exactly how INTERSECT works, its syntax requirements, and practical real-world examples to implement it effectively in your SQL workflows.

What is SQL INTERSECT and how does it work?

SQL INTERSECT is a set operator that combines the results of two SELECT queries and returns only the rows they have in common. Each row in the result appears just once, even if it occurs multiple times in the underlying tables. INTERSECT ignores duplicate rows by default, focusing strictly on unique matches between both queries.

Think of INTERSECT as a way to answer the question, “Which rows would both queries select?” For example, if you have a list of customers who placed an order this month and a separate list of customers who signed up for your newsletter, INTERSECT would give you only those customers who did both. This operation is useful for analytics, data validation, and identifying overlaps without writing complex joins or subqueries.

INTERSECT is supported in systems like PostgreSQL, Oracle, and SQL Server, but it’s not available in MySQL as of June 2024. In databases without INTERSECT, you can often use alternative approaches such as INNER JOIN or EXISTS with subqueries to achieve a similar outcome, though with some differences in handling duplicates and query structure.

SQL INTERSECT syntax and requirements

The basic syntax for using INTERSECT is straightforward. Each side of the INTERSECT must be a complete SELECT statement:

SELECT column1, column2, ...
FROM table1
WHERE condition1
INTERSECT
SELECT column1, column2, ...
FROM table2
WHERE condition2;

Both SELECT statements must return the same number of columns, and each pair of columns must have compatible data types. For example, if the first SELECT returns three columns (like id, name, email), the second SELECT must also return three columns of matching types and in the same order.

INTERSECT automatically removes duplicate rows from the results, similar to using SELECT DISTINCT. You cannot use ORDER BY directly within the individual SELECT queries; if ordering is needed, add ORDER BY to the entire INTERSECT result set at the very end. Aggregate functions, aliases, and expressions can be used within the SELECT statements, as long as the output structure matches on both sides.

Practical examples of using SQL INTERSECT

Suppose you have two tables, employees and project_members, each with columns for employee_id and name. To find which employees are also project members, you can use INTERSECT like this:

SELECT employee_id, name
FROM employees
INTERSECT
SELECT employee_id, name
FROM project_members;

The result shows only those employees who appear in both tables, eliminating anyone who is only in one. If you want to find customers who have both ordered a product and subscribed to your newsletter, the query would look like:

SELECT customer_email
FROM orders
INTERSECT
SELECT customer_email
FROM newsletter_subscribers;

This approach can also be extended to more complex queries. For instance, you might search for students who are enrolled in both the “Math” and “Science” courses by intersecting two SELECT results, each filtered for the respective course. These examples demonstrate how INTERSECT helps you isolate precisely the overlapping dataset you need for analysis or reporting.

When to use INTERSECT versus other set operators

Use INTERSECT when you specifically need rows that exist in both result sets—no more, no less. If you want all unique rows from either query, use UNION instead. If you need all rows from the first query except those also returned by the second, use EXCEPT (sometimes called MINUS in certain databases).

INTERSECT is best suited for scenarios like:

  • Finding users who meet multiple criteria spread across different tables.
  • Identifying products listed in two different catalogs.
  • Comparing lists from different time periods or sources to find consistent entries.

While INNER JOIN can also show overlapping data, INTERSECT requires no explicit join condition and works purely at the result set level. This can simplify queries and make intent clearer. However, if you need details from both tables or want to match on multiple columns with complex logic, JOINs may be more flexible. Choose the approach that best matches your data structure and reporting goals.