Joining Data in SQL
Maham Faisal Khan
Senior Content Developer, DataCamp
SELECT * FROM left_table INNER JOIN right_table ON left_table.id = right_table.idINNER JOIN another_table ON left_table.id = another_table.id;
Note. Depending on the use case, left_table or right_table can be used in the ON clause.
prime_minister_terms table from our database of world leaders
SELECT p1.country, p1.continent,
president, prime_minister
FROM prime_ministers as p1
INNER JOIN presidents as p2
USING(country);
| country | continent | president | prime_minister |
| -------- | ------------- | ----------------------- | ---------------- |
| Egypt | Africa | Abdel Fattah el-Sisi | Mostafa Madbouly |
| Portugal | Europe | Marcelo Rebelo de Sousa | Luís Montenegro |
| Pakistan | Asia | Asif Ali Zardari | Shehbaz Sharif |
| India | Asia | Droupadi Murmu | Narendra Modi |

-- SQL query for chaining inner joins SELECT p1.country, p1.continent, president, prime_minister, pm_start FROM prime_ministers as p1 INNER JOIN presidents as p2 USING(country)INNER JOIN prime_minister_terms as p3 USING(prime_minister);
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
INNER JOIN ON the id field
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id
AND left_table.date = right_table.date;
INNER JOIN ON the id AND date field
Joining Data in SQL