SQL INTERSECT
INTERSECT finds the overlap between two compatible result sets.
INTERSECT returns rows that appear in both result sets. It is a
compact way to answer "what values do these two groups have in common?"
Like UNION, it compares the selected rows, so keep the selected
columns focused on the value you want to compare.
select value_column
from first_table
intersect
select value_column
from second_table Compare the same shape
Both sides need the same number of columns. If you select a customer name on one side and a supplier name on the other, you are comparing names, not the states they share.
Schema · Garden Shop Tables · customers, suppliers 2 tables
One row per customer. Some customers have no phone on file.
One row per supplier. Some suppliers have no contact email.
Return the state values that appear in both
customers and suppliers, sorted
alphabetically.
Run your query to inspect the result.
Expected answer
- Columns: state.
- Rows: states found in both customers and suppliers.
- INTERSECT returns the overlap between two compatible result sets.
Related
Comparisons, AND/OR, IN, BETWEEN, and LIKE.
SELECT, WHERE, ORDER BY, and LIMIT at a glance.
Practice ranking, running totals, and top row per group.
Debug filters that match nothing.
Filter window results in a CTE or with QUALIFY.
Nested SELECTs, IN, EXISTS, and correlation.