Unir datos en SQL
Maham Faisal Khan
Senior Content Developer, DataCamp
INNER JOIN
en el campo id
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
Una semiunión elige los registros de la primera tabla cuando se cumple una condición en la segunda tabla.
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 |
Unir datos en SQL