Concept · Advanced SQL

INTERSECT

INTERSECT returns the rows that appear in both SELECT statements. The queries must have compatible columns in the same positions.

INTERSECT example

We want the customers who are registered in London and are also still active:

SQL
SELECT name
FROM customers
WHERE city = 'London'
INTERSECT
SELECT name
FROM customers
WHERE active = 1
ORDER BY name;

Peter Hill lives in London but does not appear, because his active value is 0. The other three people meet both conditions.

Check the result with small data

We are looking for whoever belongs to both groups at once.

Self-contained query · SQLite
WITH remote(name) AS (VALUES ('Amy'),('Liam')),
     full_time(name) AS (VALUES ('Amy'),('Eva'))
SELECT name FROM remote INTERSECT
SELECT name FROM full_time;
Query result
name
Amy

INTERSECT keeps only what the two have in common: Amy. Unlike a JOIN, this operation compares the same shape of columns across two queries.

Check your understanding: What would it return if Amy were no longer in the remote group?

Show answer

No rows: Amy would no longer be in both groups and nobody else belongs to both.

What changes from case to case

Prepare compatible queries

Both SELECT statements must return the same number of columns, with comparable types in the same positions. The final column names usually come from the first SELECT.

Structure
SELECT column_name
FROM first_source
INTERSECT
SELECT column_name
FROM second_source;

INTERSECT removes duplicates. The result lists each match once. If you need to count how many times each value appears, prepare that information first with an aggregate.

When to use a JOIN

INTERSECT compares results that are already built. A JOIN relates rows through columns and lets you bring in extra information from several tables. If you only need to know which values are in both groups, INTERSECT expresses that very clearly.

Common mix-ups with INTERSECT

Selecting different columns on each side

Do not combine, say, a numeric id with a text name just because both SELECT statements return one column. Each position must hold the same type of data and mean the same thing.

An intersection compares whole rows, not individual columns

INTERSECT returns the rows that appear in both results. The comparison covers the whole row: the same columns, in the same order, with equal values. That is why both queries must return the same number of columns with compatible types; adding a column to one of them changes the question.

The result removes duplicates: even if a customer appears several times on each side, the intersection shows them once. Nulls count as equal to each other in this operation, unlike the equality in a WHERE, where two nulls do not match.

A common alternative is EXISTS or a JOIN with DISTINCT, which is useful when you need extra columns from the first set. Test the case with duplicates on both sides and a value present on only one, and check your engine version: MySQL added INTERSECT in 8.0.31.

Self-contained case to check in SQLite

Amy and Eva buy in both January and February; Eva appears more than once in each month.

Data and query · SQLite
WITH january(customer) AS (
  VALUES ('Amy'), ('Liam'), ('Eva'), ('Eva')
), february(customer) AS (
  VALUES ('Amy'), ('Eva'), ('Eva'), ('Megan')
)
SELECT customer FROM january
INTERSECT
SELECT customer FROM february
ORDER BY customer;
Verified result of the self-contained case
customer
Amy
Eve

Liam and Megan appear in only one month and are left out. Eva repeats on each side, but the intersection returns her once.

Check your interpretation: What would the query return if you changed INTERSECT to UNION?

Show answer

Amy, Eva, Liam and Megan: UNION combines the customers from both months and removes repetitions, so Eva appears only once.

Technical questions: PostgreSQL: combining queries (UNION, INTERSECT, EXCEPT) (opens in a new tab).