INNER JOIN
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.
Unir dos tablas
Para utilizar datos de ambas tablas a la vez, las uniós. Un INNER JOIN empareja filas que coinciden en una clave —en este caso, el customer_id del pedido con el id del cliente:
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
Cuando un nombre de columna podría pertenecer a cualquiera de las dos tablas, escribe table.column para que quede 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.
Solo sobreviven las filas coincidentes
Un INNER JOIN conserva una fila solo cuando la condición ON encuentra una coincidencia en la otra tabla.
En nuestra tienda, Ben y Dan no tienen pedidos, por lo que no aparecen en la unión —no hay nada con qué emparejarlos—. Ada y Cara, que sí tienen pedidos, aparecen una vez por cada uno de sus 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.
Errores comunes
- Empareja mediante la clave usando
ON; omitir elONempareja cada fila con todas las demás. - El INNER JOIN elimina las filas que no tienen coincidencia en el otro lado.
INNER JOIN matches keys · INNER JOIN une claves coincidentes
Rows from two tables combine where the key matches. · Las filas de dos tablas se combinan donde la clave coincide.
List each customer's name next to the total of each of their orders. Join customer to · hasta orders on the key, ordered by orders.id. · Listar el name de cada cliente junto al total de cada una de sus órdenes. Unir customer con orders por la clave, ordenado por orders.id.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Show the customer name and order total for orders of 25 or more, largest total first. · Mostrar el name del cliente y el total de la orden para órdenes de 25 o más, el total más grande primero.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.