Home / SQL Tutorial / Day 11
Day 11 — Joining three tables
Two tables was the lesson. Three is the job — because that is where the answers management wants actually live.
Chaining joins
Each JOIN attaches one more table. Work left to right: Orders has the OrderID, OrderItems has the ProductID, Products has the name.
JOIN and INNER JOIN are the same thing
Writing
JOIN on its own means INNER JOIN. Most working code drops the word INNER.Calculating a line value
Multiply quantity by price to get what each line is worth.
Four tables and a GROUP BY
Which product category earns most, by customer city? This is a genuine management report, in one query.
Best-selling products
How to write one of these without getting lost
- Write
SELECT * FROMthe first table. Run it. - Add one JOIN. Run it. Check the row count is what you expect.
- Add the next JOIN. Run it again.
- Only when the joins are right, add WHERE, then GROUP BY, then ORDER BY.
If the row count jumps unexpectedly
You have a join condition that matches more rows than you thought, and every total downstream is now inflated. Stop and fix the join — do not carry on and try to correct it later.
Try these yourself
- List every order line with customer name and product name.
- Total units sold per Category.
- Revenue per SalesRep using OrderItems rather than Orders.Amount.
- The single best-selling product by units.
- Revenue per State, highest first.
Want this taught properly?
This tutorial covers the basics. Our SQL course in Noida takes you through window functions, CTEs and stored procedures on a real database, with projects and mentor support.
See the SQL course →