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.
Unindo duas tabelas
Para usar dados de ambas as tabelas ao mesmo tempo, você as une. Um INNER JOIN agrupa linhas que correspondem em uma chave — aqui, o customer_id do pedido com o id do cliente:
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
Quando um nome de coluna pode vir de qualquer uma das tabelas, escreva table.column para ficar claro.
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.
Apenas linhas correspondentes permanecem
Um INNER JOIN mantém uma linha apenas quando a condição de ON encontra uma correspondência na outra tabela.
Em nossa loja, Ben e Dan não têm pedidos, então eles não aparecem no join — não há nada com o que pareá-los. Ada e Cara, que têm pedidos, aparecem uma vez para cada um de seus pedidos.
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.
Erros comuns
- Corresponda pela chave com
ON; umaONausente pareia cada linha com todas as linhas. - INNER JOIN descarta linhas sem correspondência no outro lado.
INNER JOIN matches keys · INNER JOIN combina chaves
Rows from two tables combine where the key matches. · Linhas de duas tabelas se combinam onde a chave corresponde.
List each customer's name next to the total of each of their orders. Join customer to · até orders on the key, ordered by orders.id. · Liste a name de cada cliente ao lado da total de cada um de seus pedidos. Junte customer a orders pela chave, ordenado por orders.id.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Show the customer name and order total for orders of 25 or more, largest total first. · Exiba o name do cliente e a total do pedido para pedidos de 25 ou mais, total maior primeiro.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.