INTERSECT
- SQL course
- Intermediate
- Lesson 61/115
- 4 min
Take rows common to two queries.
INTERSECT
INTERSECT returns the intersection of two SELECT results. Rows found in both queries are returned.
SELECT name FROM customers
INTERSECT
SELECT name FROM leads;This query returns those who are both customers and leads.
name is selected from customers.
INTERSECT — details
INTERSECT keeps only rows present in both sets. Duplicates are automatically removed.
For example, to find companies that are both customers and suppliers, you intersect the two lists with INTERSECT.
Practice INTERSECT
Find those who are both customers and leads using INTERSECT.
Starter code
-- intersection
SELECT name FROM customersSample solution
SELECT name FROM customers
INTERSECT
SELECT name FROM leads;Expected output
name Ayşe
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 INTERSECT: match the terms
| Term | Meaning |
|---|---|
INTERSECT |
intersection |
UNION |
union |
EXCEPT |
difference |
UNION ALL |
with duplicates |
2 Add INTERSECT
SELECT name FROM customers
____
SELECT name FROM leads;
SELECT name FROM customers
INTERSECT
SELECT name FROM leads;
Explanation: INTERSECT is the right answer.
3 What does INTERSECT keep?
- All of them
- Only the right
- Only the left
- Rows present in both Correct answer
Explanation: INTERSECT keeps rows in both queries.