How to JOIN Three Tables in SQL
Most real queries need data from 3+ tables. Here's the clean pattern for multi-table JOINs that actually performs well.
Formula Genius can generate a draft from a plain-English prompt. Supported formulas can be checked with sample cases; review the coverage on the result.
The formula
"Get order details with customer name and product name from three tables"
SELECT o.order_id, c.name AS customer, p.name AS product, o.quantity
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON o.product_id = p.id
WHERE o.created_at >= '2026-01-01'
ORDER BY o.created_at DESC;
This query pulls from three tables — orders, customers, and products — joining them on their foreign key relationships to build a complete picture of each order.
Step-by-step breakdown
- FROM orders o — start with the orders table (aliased as 'o')
- INNER JOIN customers c ON o.customer_id = c.id — add customer names
- INNER JOIN products p ON o.product_id = p.id — add product names
- WHERE filters to orders from 2026 onward
- ORDER BY sorts newest orders first
Edge cases & warnings
- INNER JOIN excludes orders with no matching customer or product
- Use LEFT JOIN to include orders even without a matching record
- Missing indexes on join columns can make this query very slow
- NULL values in join columns are never matched by JOIN
Examples
"3 tables: orders, customers, products"
Combined view with customer name + product name per order
"Add LEFT JOIN for customers"
Includes orders even if customer was deleted
Frequently asked questions
How many tables can I JOIN?
Technically unlimited, but each JOIN adds performance cost. Most queries join 3-5 tables. If you need 10+, consider restructuring your data or using CTEs for clarity.
INNER JOIN vs LEFT JOIN — which should I use?
INNER JOIN: only show rows with matches in BOTH tables. LEFT JOIN: show all rows from the left table, even without matches. Use LEFT JOIN when data might be missing.
Can't find what you need?
Describe a formula in plain English. Formula Genius drafts and explains it, and can check supported formulas on sample cases.