Brightlane's sales team needs a line-item report showing each order alongside the customer who placed it and the quantity of each item in that order.
Write a query to return the order ID, customer name, and quantity for every order line.
Assumptions:
- The result row count is one row per order line item (not one row per order).
Output:
- One row per order line, with columns
order_id,customer_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,
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_id The shape
Two JOINs chain three tables on shared keys — orders to customers by customer_id, then orders to order_items by order_id. The result lands at one row per line item, because order_items is the most-multiplying table in the chain.
Clause by clause
SELECT o.id AS order_id, c.name AS customer_name, oi.quantitypicks one column from each of the three tables. Every reference is alias-prefixed, which is the only way the bare namesidandnamestay unambiguous in a chain where bothordersandcustomershave a column calledid.FROM orders oanchors the chain on orders and aliases the table aso.JOIN customers c ON o.customer_id = c.idattaches the customer to each order through the foreign key. Each order has one customer, so this join doesn't change the row count.JOIN order_items oi ON o.id = oi.order_idthen attaches every line item belonging to that order. This is the multiplying step — an order with three items becomes three rows. The first row pair for Alice Nguyen's order1shows two rows because that order has two line items.
You practiced chaining three tables in a single FROM clause with two JOIN clauses. The recurring shape: each JOIN adds one table; the ON clause specifies how that new table connects to the working set so far.