Data is split across tables so we need to bring it together in one result. JOIN links the rows of two tables through a shared column (a foreign key and its primary key).
INNER JOIN
Each order with its customer's name. The condition after ON names the matching columns; o and c are table aliases. Open in SQL Lab
LEFT JOIN
LEFT JOIN returns every row of the left table even without a match, filling the right side with NULL. Very useful for finding what is missing.
All customers with their order count, including zero. Open in SQL Lab
Customers who never ordered: a row with no match on the right. Open in SQL Lab
JOIN with aggregation
Revenue per category: join items to products, then aggregate. Open in SQL Lab
For advanced readers How the database executes a join
The database picks a join algorithm (nested loop, hash join or merge join) based on sizes and indexes. An index on the join column (the foreign key) speeds queries a lot. Use EXPLAIN to see the real plan.
Exercise 1 — Show the order id and customer name for the first 5 orders by id: columns id and name.
JOIN, ON, then ORDER BY o.id.
Exercise 2 — How many customers have no orders at all?
LEFT JOIN, WHERE o.id IS NULL, then COUNT(*).
Exercise 3 — Show revenue per product category: category and revenue (quantity × unit_price).
Join order_items to products then GROUP BY category.
- JOIN links tables through a shared column.
- INNER shows matches only; LEFT keeps every left row.
- LEFT JOIN + IS NULL finds what is missing.
Want an expert to do it for you?
Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.