CodeBlanks Logo Icon CodeBlanks

INTERSECT

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

query.sql
-- intersection
SELECT name FROM customers

Sample solution

query.sql
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

Question
SELECT name FROM customers
____
SELECT name FROM leads;
Answer
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.

All lessons in Intermediate