To join three or more tables in MySQL, chain several JOIN ... ON clauses one after another: each new join connects one more table to any table already in the query. The mysql join multiple tables pattern is the same as a two-table join repeated, and MySQL evaluates the joins from left to right, each one building on the result so far.
This guide uses four tables: customers(id, name), orders(id, customer_id, created_at), order_items(order_id, product_id, quantity, unit_price) and products(id, name, category_id).
Think of each join as adding one more table to a growing result. Start with orders and customers, then add the items, then the products:
Each ON condition links the new table to a table that already appeared. Every ON clause belongs to the join right before it.
You can mix INNER and LEFT joins in one query. Suppose you want every customer, even those with no orders, but items and products only where they exist:
Once you use a LEFT JOIN, keep the following joins as LEFT JOINs too. An INNER JOIN placed after a LEFT JOIN that uses the left-side NULL columns removes the unmatched rows again and quietly turns the query into an inner join.
Joining one-to-many tables multiplies rows: one order with three items produces three rows. That is correct for listing, but it breaks sums taken from the "one" side:
Here o.total repeats once per item, so the sum is inflated. Aggregate the column that belongs to the many side, or compute it from items:
When you must combine two separate one-to-many relations, for example items and payments for the same order, aggregate each in a subquery first and join the results, otherwise the rows multiply against each other.
A junction table connects two tables with a many-to-many relation. Students and courses, or products and tags, always need three tables:
Always join through the junction table; there is no direct link between the first and last table.
Follow a few habits so long joins stay understandable:
ON condition next to it.SELECT * across many tables.Join conditions can also use several columns, for example a composite key: ON a.x = b.x AND a.y = b.y.
Every join column should be indexed: primary keys are indexed automatically, and foreign key columns such as orders.customer_id and order_items.order_id need an index too (InnoDB creates one when you declare the foreign key). Use EXPLAIN to confirm each table uses an index lookup. The MySQL optimizer is free to reorder inner joins for speed, so the written order does not fix the execution order; for LEFT JOINs the order matters for the result and is respected. The limit on tables in one join is 61.
Up to 61 tables in a single join, though queries with more than a handful are hard to read and tune.
For inner joins the result is the same in any order. For outer joins, order and the table written on the left change which rows are kept.
One-to-many joins repeat parent rows. Aggregate the child table, or pre-aggregate in a subquery before joining.
A view is useful when many queries need the same join, since it hides the joins behind one name.
Join three or more tables by chaining JOIN ... ON clauses, each linking a new table to one already present. Keep join types consistent after a LEFT JOIN, watch for row multiplication when you aggregate, go through junction tables for many-to-many data, and index every join column.