At the INTERSECT

Unire i dati in 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.

Unire i dati in 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.

Unire i dati in SQL

INTERSECT syntax

SELECT id, val
FROM left_table

INTERSECT
SELECT id, val FROM right_table;
Unire i dati in 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 *
FROM left_table
INNER JOIN right_table
ON left.id = right.id
    AND left.val = right.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.

Unire i dati in 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             |
Unire i dati in SQL

INTERSECT on two fields

SELECT country, prime_minister AS leader
FROM prime_ministers
INTERSECT
SELECT country, president
FROM presidents;
|-----------|----------|
| country   | leader   |
|-----------|----------|
|-----------|----------|
Unire i dati in 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  |
|---------|--------------------|
Unire i dati in SQL

Let's practice!

Unire i dati in SQL

Preparing Video For Download...