Jointure de données dans SQL
Maham Faisal Khan
Senior Content Developer, DataCamp




Les dirigeants du monde jusqu’en 2024 :

SELECT *
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 |



--Inner join of presidents and prime_ministers, joining on countrySELECT prime_ministers.country, prime_ministers.continent, prime_minister, presidentFROM presidentsINNER JOIN prime_ministersON presidents.country = prime_ministers.country;
Remarque : le format table.nom_colonne doit être utilisé pour sélectionner des colonnes qui existent dans les deux tables afin d’éviter une erreur SQL.
| country | continent | prime_minister | president |
|----------------|-----------|------------------|-------------------------|
| Egypt | Africa | Mostafa Madbouly | Abdel Fattah el-Sisi |
| Portugal | Europe | Luís Montenegro | Marcelo Rebelo de Sousa |
| Pakistan | Asia | Shehbaz Sharif | Asif Ali Zardari |
| India | Asia | Narendra Modi | Ram Nath Kovind |
--Inner join of presidents and prime_ministers, joining on countrySELECT p2.country, p2.continent, prime_minister, presidentFROM presidents AS p1 INNER JOIN prime_ministers AS p2ON p1.country = p2.country;
| country | continent | prime_minister | president |
|----------------|-----------|------------------|-------------------------|
| Egypt | Africa | Mostafa Madbouly | Abdel Fattah el-Sisi |
| Portugal | Europe | Luís Montenegro | Marcelo Rebelo de Sousa |
| Pakistan | Asia | Shehbaz Sharif | Asif Ali Zardari |
| India | Asia | Narendra Modi | Ram Nath Kovind |
Les alias peuvent être utilisés dans la syntaxe table.nom_colonne dans les clauses SELECT et ON.
--Inner join of presidents and prime_ministers, joining on country
SELECT p2.country, p2.continent, prime_minister, president
FROM presidents AS p1
INNER JOIN prime_ministers AS p2
USING(country);
| country | continent | prime_minister | president |
|----------------|-----------|------------------|-------------------------|
| Egypt | Africa | Mostafa Madbouly | Abdel Fattah el-Sisi |
| Portugal | Europe | Luís Montenegro | Marcelo Rebelo de Sousa |
| Pakistan | Asia | Shehbaz Sharif | Asif Ali Zardari |
| India | Asia | Narendra Modi | Ram Nath Kovind |
Jointure de données dans SQL