Dès qu'une base de données compte plus d'une table, il faut savoir les combiner. C'est le rôle des jointures (JOIN). Ce tutoriel couvre les quatre types de jointures les plus utilisés, avec des exemples que vous pouvez tester directement.
As soon as a database has more than one table, you need to know how to combine them. That's the role of joins (JOIN). This tutorial covers the four most commonly used join types, with examples you can test directly.
Les tables d'exemple
The Example Tables
Pour illustrer chaque jointure, on utilise deux tables simples : clients et commandes.
To illustrate each join, we'll use two simple tables: customers and orders.
| id | nom |
|---|---|
| 1 | Amina |
| 2 | Karim |
| 3 | Julie |
| id | name |
|---|---|
| 1 | Amina |
| 2 | Karim |
| 3 | Julie |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 49.90 |
| 102 | 1 | 12.00 |
| 103 | 4 | 8.50 |
| id | customer_id | amount |
|---|---|---|
| 101 | 1 | 49.90 |
| 102 | 1 | 12.00 |
| 103 | 4 | 8.50 |
Remarquez que Julie (id 3) n'a aucune commande, et que la commande 103 appartient à un client_id 4 qui n'existe pas dans clients. C'est volontaire, pour bien voir la différence entre chaque jointure.
Notice that Julie (id 3) has no orders, and that order 103 belongs to customer_id 4, which doesn't exist in customers. This is intentional, so you can clearly see the difference between each join.
INNER JOIN : uniquement les correspondances
INNER JOIN: Matches Only
INNER JOIN ne retourne que les lignes où la condition de jointure trouve une correspondance dans les deux tables.
INNER JOIN only returns rows where the join condition finds a match in both tables.
SELECT c.nom, co.montant
FROM clients c
INNER JOIN commandes co ON co.client_id = c.id;
SELECT c.name, o.amount
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;
Résultat : Amina apparaît deux fois (une par commande), Karim et Julie n'apparaissent pas car ils n'ont pas de commande correspondante des deux côtés.
Result: Amina appears twice (once per order); Karim and Julie don't appear because they have no matching order on both sides.
LEFT JOIN : tout de la table de gauche
LEFT JOIN: Everything from the Left Table
LEFT JOIN (ou LEFT OUTER JOIN) garde toutes les lignes de la table de gauche, même sans correspondance à droite. Les colonnes manquantes sont remplies avec NULL.
LEFT JOIN (or LEFT OUTER JOIN) keeps every row from the left table, even without a match on the right. Missing columns are filled with NULL.
SELECT c.nom, co.montant
FROM clients c
LEFT JOIN commandes co ON co.client_id = c.id;
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
Résultat : Amina (x2), Karim avec montant = NULL, Julie avec montant = NULL. C'est la jointure la plus utilisée en pratique, notamment pour trouver les lignes "orphelines" (par exemple, les clients sans aucune commande) avec un WHERE co.id IS NULL.
Result: Amina (x2), Karim with amount = NULL, Julie with amount = NULL. This is the most commonly used join in practice, especially for finding "orphan" rows (e.g., customers with no orders at all) using a WHERE o.id IS NULL.
RIGHT JOIN : tout de la table de droite
RIGHT JOIN: Everything from the Right Table
RIGHT JOIN fait l'inverse : il garde toutes les lignes de la table de droite.
RIGHT JOIN does the opposite: it keeps every row from the right table.
SELECT c.nom, co.montant
FROM clients c
RIGHT JOIN commandes co ON co.client_id = c.id;
SELECT c.name, o.amount
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id;
Résultat : Amina (x2), et la commande 103 apparaît avec nom = NULL puisqu'aucun client n'a l'id 4.
Result: Amina (x2), and order 103 appears with name = NULL since no customer has id 4.
RIGHT JOIN en LEFT JOIN en inversant l'ordre des tables. Cela rend les requêtes plus faciles à lire de gauche à droite.
Tip: in practice, most developers prefer to rewrite a RIGHT JOIN as a LEFT JOIN by swapping the table order. This makes queries easier to read from left to right.
FULL JOIN : tout, des deux côtés
FULL JOIN: Everything, from Both Sides
FULL JOIN (ou FULL OUTER JOIN) combine LEFT et RIGHT : toutes les lignes des deux tables apparaissent, avec des NULL là où il n'y a pas de correspondance.
FULL JOIN (or FULL OUTER JOIN) combines LEFT and RIGHT: every row from both tables appears, with NULL wherever there's no match.
SELECT c.nom, co.montant
FROM clients c
FULL JOIN commandes co ON co.client_id = c.id;
SELECT c.name, o.amount
FROM customers c
FULL JOIN orders o ON o.customer_id = c.id;
Note : MySQL ne supporte pas FULL JOIN nativement ; il faut simuler ce comportement avec un UNION d'un LEFT JOIN et d'un RIGHT JOIN.
Note: MySQL doesn't support FULL JOIN natively; you have to simulate this behavior with a UNION of a LEFT JOIN and a RIGHT JOIN.
Résumé visuel
Visual Summary
| Type | Ce qu'il retourne |
|---|---|
| INNER JOIN | Uniquement les lignes qui correspondent des deux côtés |
| LEFT JOIN | Tout à gauche + correspondances à droite (NULL sinon) |
| RIGHT JOIN | Tout à droite + correspondances à gauche (NULL sinon) |
| FULL JOIN | Tout des deux côtés, avec NULL là où ça ne correspond pas |
| Type | What It Returns |
|---|---|
| INNER JOIN | Only rows that match on both sides |
| LEFT JOIN | Everything on the left + matches on the right (NULL otherwise) |
| RIGHT JOIN | Everything on the right + matches on the left (NULL otherwise) |
| FULL JOIN | Everything from both sides, with NULL where there's no match |
ON sont indexées, surtout sur les grandes tables. Voir notre article sur les index SQL pour aller plus loin.
Performance tip: make sure the columns used in the ON condition are indexed, especially on large tables. See our article on SQL indexes to go further.
