Lesson 15.3
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.
Pattern
select value_column
from first_table
intersect
select value_column
from second_tableCompare 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 ShopTable · customers9 columns · 20 rows
Table · customers
9 columns · 20 rowsOne row per customer. Some customers have no phone on file.
customer_id intfirst_name textlast_name textemail textphone textcity textstate textsignup_date dateis_active bool
Your task
Return the state values that appear in bothcustomers and suppliers, sorted alphabetically.
SQL Workbench
⌘↵ to run·
Expected answer
- Columns: state.
- Rows: states found in both customers and suppliers.
- INTERSECT returns the overlap between two compatible result sets.