At the INTERSECT

Joindre des données en SQL

Maham Faisal Khan

Senior Content Developer, DataCamp

INTERSECT Venn diagram

An image that shows three Venn diagrams, with two faded out. Only the diagram with INTERSECT is in focus. The INTERSECT diagram has two overlapping circles, with only the overlapping part colored in green.

Joindre des données en SQL

INTERSECT diagram

A diagram showing two tables, left and right. On the right side of the diagram, there is an illustration of what happens when a INTERSECT operation is performed on the two tables.

Joindre des données en SQL

INTERSECT syntax

SELECT id, val
FROM left_table

INTERSECT
SELECT id, val FROM right_table;
Joindre des données en SQL

INTERSECT vs. INNER JOIN on two columns

INTERSECT syntax

SELECT *
FROM left_table
INTERSECT
SELECT *
FROM right_table;

A diagram showing two tables, left and right. On the right side of the diagram, there is an illustration of what happens when a UNION operation is performed on the two tables.

INNER JOIN syntax

SELECT l.id, l.val
FROM left_table AS l
INNER JOIN right_table AS r
  ON l.id = r.id
      AND l.val = r.val;

A diagram showing two tables, left and right. On the right side of the diagram, there is an illustration of what happens when an INNER JOIN is performed on the two tables.

Joindre des données en SQL

Countries with prime ministers and presidents

SELECT country as intersect_country
FROM prime_ministers
INTERSECT
SELECT country
FROM presidents;
| intersect_country |
|-------------------|
| Egypt             |
| Portgual          |
| Pakistan          |
| India             |
Joindre des données en SQL

INTERSECT on two fields

SELECT country, prime_minister AS leader
FROM prime_ministers
INTERSECT
SELECT country, president
FROM presidents;
|-----------|----------|
| country   | leader   |
|-----------|----------|
|-----------|----------|
Joindre des données en SQL

Countries with prime ministers and monarchs

SELECT country, prime_minister AS leader
FROM prime_ministers
INTERSECT
SELECT country, monarch
FROM monarchs;
|---------|--------------------|
| country | leader             |
|---------|--------------------|
| Brunei  | Hassanal Bolkiah   |
| Oman    | Haitham bin Tarik  |
|---------|--------------------|
Joindre des données en SQL

Let's practice!

Joindre des données en SQL

Preparing Video For Download...