Intersect and Except in SQL: How They Work

How-to basics · Updated November 23, 2026

In SQL, INTERSECT returns only the rows that appear in both query results, while EXCEPT returns rows from the first query that are not present in the second query. Both operators compare entire rows (all selected columns) and remove duplicates by default, functioning as set operations on query outputs.

Background: Where These Operators Come From

SQL is built on relational algebra, which includes set operations like union, intersection, and difference. Early SQL standards (SQL-92) formalized INTERSECT and EXCEPT as part of the language, though many database systems implemented them later. EXCEPT is the standard SQL name, but some databases—notably Oracle and PostgreSQL—use MINUS as a synonym for the same operation. Microsoft SQL Server and SQLite use EXCEPT.

The key idea is that these operators work on the result sets of two SELECT statements, not on tables directly. You can use them with subqueries, joins, or any valid query. They are especially useful for data comparison tasks, such as finding records that exist in one system but not another, or identifying common entries across two datasets.

How INTERSECT Works

INTERSECT takes two queries and returns only the rows that are present in both. It implicitly removes duplicates: if a row appears multiple times in either query, it appears only once in the final result. The two queries must have the same number of columns, and corresponding columns must have compatible data types.

Example: Suppose you have two tables, current_customers and vip_customers, both with columns customer_id and email. To find customers who are both current and VIP:

SELECT customer_id, email FROM current_customers
INTERSECT
SELECT customer_id, email FROM vip_customers;

This returns only the rows that match exactly on both columns. If a customer appears in both tables but with a different email, they won’t be included because the entire row must match.

Note that INTERSECT is not the same as an inner join. A join can return columns from both tables and can match on a subset of columns. INTERSECT compares whole rows and returns only the columns you selected, with no additional columns from the second query.

How EXCEPT Works

EXCEPT returns rows from the first query that are not found in the second query. It also removes duplicates. The column count and types must match between the two queries.

Example: To find customers who are current but not VIP:

SELECT customer_id, email FROM current_customers
EXCEPT
SELECT customer_id, email FROM vip_customers;

This returns all current customers that do not appear as VIP customers. If a current customer appears in the VIP table with a different email, they will be included because the row doesn’t match exactly.

Order matters: EXCEPT is not commutative. Reversing the queries would give you VIP customers who are not current. In contrast, INTERSECT is commutative—the order of queries doesn’t change the result.

Comparing INTERSECT, EXCEPT, and JOINs

Often you can achieve the same results with joins or NOT EXISTS, but the set operators are more concise and readable for simple comparisons. Here’s a quick comparison:

  • INTERSECT vs. INNER JOIN: A join can return columns from both tables and can match on a key. INTERSECT only returns whole rows from the first query. If you need columns from both tables, use a join.
  • EXCEPT vs. LEFT JOIN with NULL check: A LEFT JOIN that filters for NULL in the second table can mimic EXCEPT, but the join may be more flexible if you need to compare on specific columns or return additional data.
  • EXCEPT vs. NOT EXISTS: NOT EXISTS is often faster on large datasets because it can short-circuit. EXCEPT requires sorting or hashing to remove duplicates. For simple existence checks, NOT EXISTS is usually preferred.

Example using NOT EXISTS to replicate the earlier EXCEPT:

SELECT customer_id, email FROM current_customers c
WHERE NOT EXISTS (
    SELECT 1 FROM vip_customers v
    WHERE v.customer_id = c.customer_id AND v.email = c.email
);

This returns the same rows but uses a correlated subquery. Performance depends on indexing and data distribution.

Common Problems and Misconceptions

One frequent mistake is forgetting that INTERSECT and EXCEPT compare entire rows. If you only want to compare on a key column, you must select only that column in both queries. For example, to find customers who are VIP regardless of email, you would select only customer_id.

Another issue is data type compatibility. Some databases require exact type matches; others implicitly convert. For instance, comparing an integer column to a string column may cause an error. Always ensure column types align.

Duplicate handling can surprise users. Both operators return distinct rows. If you need to preserve duplicates, you must use UNION ALL with a different approach, because there is no INTERSECT ALL or EXCEPT ALL in standard SQL. Some databases like PostgreSQL support INTERSECT ALL and EXCEPT ALL, but SQL Server and Oracle do not.

Finally, remember that EXCEPT is not the same as NOT IN when NULLs are involved. NOT IN returns no rows if the subquery contains a NULL, whereas EXCEPT handles NULLs as comparable values. For example, if the VIP table has a NULL email, NOT IN would fail, but EXCEPT would still work correctly.

Performance and Indexing Considerations

Set operators can be expensive on large tables because they often require sorting or hashing to eliminate duplicates. To optimize:

  • Use indexes on the columns you are comparing, especially if you use EXCEPT with a large first query.
  • Limit the number of columns in the SELECT list—fewer columns mean less data to compare.
  • Consider using EXISTS or NOT EXISTS for simple existence checks, as they can stop early.
  • In SQL Server, INTERSECT and EXCEPT are implemented with a merge join or hash match; ensure statistics are up to date.

For very large datasets, test both the set operator and the equivalent join/EXISTS query to see which performs better in your database.

Database-Specific Syntax and Support

Most major databases support both operators, but there are nuances:

  • Microsoft SQL Server: Supports INTERSECT and EXCEPT (not MINUS).
  • Oracle: Supports INTERSECT and MINUS (not EXCEPT).
  • PostgreSQL: Supports INTERSECT, EXCEPT, and also MINUS as an alias.
  • MySQL: Did not support these operators until MySQL 8.0.19, which added INTERSECT and EXCEPT (though MINUS is also accepted).
  • SQLite: Supports INTERSECT and EXCEPT.

Always check your database version documentation, as older versions may not have these operators.

Frequently Asked Questions

Can I use INTERSECT and EXCEPT with more than two queries?

Yes, you can chain them. For example, SELECT ... INTERSECT SELECT ... INTERSECT SELECT ... works, and the operations are left-associative. Similarly, you can chain EXCEPT. However, mixing them requires parentheses to clarify order, as they have the same precedence in most databases.

What is the difference between EXCEPT and NOT IN?

EXCEPT compares whole rows and handles NULLs correctly, while NOT IN compares a single column and can behave unexpectedly if the subquery returns any NULL. Also, EXCEPT removes duplicates, while NOT IN does not.

Do INTERSECT and EXCEPT work with NULL values?

Yes, they treat NULLs as equal to other NULLs. So a row with a NULL in a column can match another row with a NULL in the same column. This is different from the = operator, where NULL = NULL is unknown.

Can I use ORDER BY with INTERSECT or EXCEPT?

Yes, you can add an ORDER BY at the end of the entire statement. The column names in the ORDER BY must refer to columns in the final result set. For example: SELECT ... INTERSECT SELECT ... ORDER BY customer_id;

Are INTERSECT and EXCEPT faster than JOINs?

Not necessarily. It depends on the database, indexes, and data size. Set operators often need to sort or hash to remove duplicates, while joins can sometimes use indexes more efficiently. Test both on your data.

What if the two queries have different column names?

That’s fine. The column names in the result set come from the first query. The data types must be compatible, but the names can differ.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.