Joining Multiple Tables in SQL
Joining multiple tables extends the two-table join pattern by chaining additional JOIN clauses, each introducing a new table into the working result set. The result of each join becomes the input for the next.
You can chain as many JOINs as you need in a single query, adding one table at a time.
Most reporting queries need more than two tables. A full order report needs customer names, product names, quantities, and order totals — each piece of data in a different table. You add one JOIN per table, each with its own ON clause. Each new table connects to something already in the query: to the driving table, or to a table you already joined.
The approach scales to as many tables as you need. Here is what three tables joined together looks like: orders, customers, and order line items in one query:
SELECT o.id AS order_id, c.name AS customer_name, oi.quantity
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN order_items oi ON o.id = oi.order_idThe first JOIN connects orders to customers via customer_id. The second JOIN connects orders to order_items via order_id. Each JOIN adds columns from a new table, and the query now has all three tables' data in each result row.
Add another JOIN to pull in product names:
SELECT o.id AS order_id, p.name AS product_name, oi.quantity, oi.unit_price
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.idHere the second JOIN connects to order_items, not to orders — because order_items has the product_id foreign key. Each ON clause specifies which columns match between two tables already in the query.
You can mix JOIN types. Some tables in the chain may have nullable foreign keys. Categories might be missing for some products; use LEFT JOIN for those:
Products with no category still appear. Their category_name column is NULL. The three INNER JOINs before it ensure orders, customers, line items, and products are all matched; the LEFT JOIN only relaxes the constraint for categories.
The one thing that trips people up: getting the ON clauses wrong.
Each JOIN's ON clause must connect a column from the new table to a column from a table already in the query. A common mistake is connecting two tables that don't share a direct relationship — JOIN products p ON p.id = c.id accidentally matches product IDs to customer IDs. Check your foreign key relationships before writing the ON clause, and verify that the row counts in the result look reasonable before relying on the output.
9 Joining Multiple Tables practice problems
Write a query to return the order ID, customer name, and quantity for every order line.
Write a query to return the order ID, product name, and quantity for every line item on record.
Write a query to return the employee name, department name, and salary amount for every salary record on file.
Write a query to return the customer name, product name, quantity, and unit price for every line item.
Write a query to return the customer name, product name, and category name for every line item where the product has a resolving category.
Write a query to return the customer name, product name, and unit price for every item in a delivered order.
Write a query to return the employee name, department name, and salary amount for every salary record belonging to an Engineering employee.
Write a query to return the customer name, order ID, and item ID for every row in the result.
Write a query to return the customer name, product name, category name, and quantity for every line item.
These problems are part of the Joining Multiple Tables lesson in SQLMaxx, with instant grading and a worked solution on each.
Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.
That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.
Real problems. Not toy examples.
615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.
Write a query. Know if it's right in one second.
No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.
Stop guessing whether you actually know it.
SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.
Learn it once. Keep it for good.
Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.
Practice, feedback, mastery, review. That's the loop that turns reading into real skill.
Start freeNo account, no credit card. Start solving in under a minute.