Brightlane's product team wants to see each order line item alongside the name of the product it represents.
Write a query to return the order ID, product name, and quantity for every line item on record.
Output:
- One row per order line item, with columns
order_id,product_name, andquantity.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
o.id AS order_id,
p.name AS product_name,
oi.quantity
FROM
orders o
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id The shape
The chain pivots through order_items, the junction table — each line item connects on one side to its order and on the other to its product. Two JOINs on shared keys land the result at one row per line item with the product's name attached.
Clause by clause
SELECT o.id AS order_id, p.name AS product_name, oi.quantitypicks one column from each table.p.nameis the product name;o.idis the order ID. Without the aliases, both names would be ambiguous — every table in the schema has anid, and bothproductsand others have aname.FROM orders oanchors the chain onorders.JOIN order_items oi ON o.id = oi.order_idattaches the line items. This is the multiplying step: an order with three line items becomes three rows in the intermediate result.JOIN products p ON oi.product_id = p.idthen resolves each line item to its product. Each line item points at exactly one product, so this join enriches the rows without multiplying them further. The row count is settled by theorder_itemsjoin above.
You practiced a three-table chain through a junction (order_items) to its two parent tables. The recurring shape: junctions almost always connect to multiple dimensions, so they're frequently the middle node in a multi-table join.