INNER JOIN
HandoutJoining 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:
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
When a column name could come from either table, write table.column so it is clear.
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.
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.
Click Run to see the output here.
Show the customer name and order total for orders of 25 or more, largest total first.
Click Run to see the output here.