Combine tables with JOIN
Real data rarely lives in one table. Users go in a users table, their orders
go in an orders table, and each order points back to the user who placed it
with a user_id column. To answer "which user bought what", you need both
tables at once - that's what JOIN is for.
JOIN ... ON matches rows across tables where a condition holds, usually an id
column on one side equalling a foreign key on the other. Each matched pair
becomes one row in the result, so you can select columns from either table as
if they'd always been in the same place.
There are now two tables: users (id, name, country, age) and orders
(id, user_id, item, amount), where orders.user_id points at users.id.
Your task: return the user's name and the item they ordered, for every
order.
You'll practice:
- Joining two tables with
JOIN ... ON
- Selecting columns from both sides of the join
Related reading: Why Joins Exist →