Joining two tables
To use data from both tables at once, you join them. An INNER JOIN pairs up rows that match on a key — here, the order's customer_id with the customer's id:
When a column name could come from either table, write table.column so it is clear.
Joindre deux tables
Pour utiliser des données provenant de les deux tables en même temps, vous devez les joindre. Un INNER JOIN associe les lignes qui correspondent sur une clé — ici, la customer_id de la commande avec la id du client :
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
Lorsqu'un nom de colonne pourrait provenir de l'une ou l'autre table, écrivez table.column pour clarifier.
Only matching rows survive
An INNER JOIN keeps a row only when the ON condition finds a match in the other table.
In our shop, Ben and Dan have no orders, so they do not appear in the join — there is nothing to pair them with. Ada and Cara, who do have orders, appear once for each of their orders.
Seules les lignes correspondantes subsistent
Un INNER JOIN conserve une ligne uniquement lorsque la condition ON trouve une correspondance dans l'autre table.
Dans notre boutique, Ben et Dan n'ont aucune commande, donc ils n'apparaissent pas dans le joint — il n'y a rien à leur associer. Ada et Cara, qui ont des commandes, apparaissent une fois pour chacune de leurs commandes.
Common mistakes
- Match on the key with
ON; a missingONpairs every row with every row. - INNER JOIN drops rows with no match on the other side.
Erreurs courantes
- Correspondre sur la clé avec
ON; une absence deONassocie chaque ligne à toutes les autres lignes. - INNER JOIN élimine les lignes sans correspondance de l'autre côté.
INNER JOIN matches keys · INNER JOIN fait correspondre les clés
Rows from two tables combine where the key matches. · Les lignes de deux tables se combinent où la clé correspond.
List each customer's name next to the total of each of their orders. Join customer to · à orders on the key, ordered by orders.id. · Répertoriez la name de chaque client à côté de la total de chacune de ses commandes. Jointez customer à orders sur la clé, trié par orders.id.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Show the customer name and order total for orders of 25 or more, largest total first. · Affichez la name du client et la total de la commande pour les commandes de 25 ou plus, du total le plus élevé au plus bas.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.