Joining Data in SQL
Maham Faisal Khan
Senior Content Developer, DataCamp
INNER JOIN
on the id
fieldSELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
A semi join chooses records in the first table where a condition is met in the second table.
SELECT country, continent, president
FROM presidents;
| country | continent | president |
| -------- | ------------- | ----------------------- |
| Egypt | Africa | Abdel Fattah el-Sisi |
| Portugal | Europe | Marcelo Rebelo de Sousa |
| USA | North America | Joe Biden |
| Uruguay | South America | Luis Lacalle Pou |
| Pakistan | Asia | Asif Ali Zardari |
| Chile | South America | Gabriel Boric |
| India | Asia | Droupadi Murmu |
SELECT country
FROM states
WHERE indep_year < 1800;
|----------|
| country |
|----------|
| Portugal |
| Spain |
|----------|
SELECT president, country, continent
FROM presidents
WHERE country IN
(SELECT country
FROM states
WHERE indep_year < 1800);
|-------------------------|-----------|-------------|
| president | country | continent |
|-------------------------|-----------|-------------|
| Marcelo Rebelo de Sousa | Portugal | Europe |
|-------------------------|-----------|-------------|
SELECT country, president
FROM presidents
WHERE continent LIKE '%America'
AND country NOT IN
(SELECT country
FROM states
WHERE indep_year < 1800);
| country | president |
| -------- | ---------------- |
| Uruguay | Luis Lacalle Pou |
| Chile | Gabriel Boric |
Joining Data in SQL