INNER JOIN with Multiple Tables
Performs INNER JOINs across three tables to combine related data from customers, orders, and products with proper table aliasing and filtering.
Code
SELECT
c.customer_id,
c.first_name,
c.last_name,
o.order_id,
o.order_date,
p.product_name,
oi.quantity,
oi.unit_price,
oi.quantity * oi.unit_price AS line_total
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id
WHERE o.order_date >= '2025-01-01'
AND o.status = 'completed'
ORDER BY o.order_date DESC, c.last_name ASC;Inside the snippet
The statement is SELECT, touching customers, orders, order_items and products.
Breaking the statements into their working parts: 3 JOINs combining rows across tables; a WHERE clause that picks out the rows one statement touches; an ORDER BY that fixes the output order.
What to watch for
- 3 joins multiply the intermediate row count; confirm the join keys are indexed before pointing this at production-sized tables.
Putting it to use
Run the statement against the customers table.
FAQ
- Which tables or CTEs does it reference?
- `customers`, `orders`, `order_items`, `products`, combined through 3 joins.
Report a problem with this exampleOpens a pre-filled issue on GitHub.