InterviewPrepKit

Home / SQL / Joins

Multi-Table Join: Customers, Orders, and Products

medium
Solving tips
  • Chain joins one table at a time, always joining the new table on a key already present in the growing result.
  • Each ON clause connects exactly two tables; the orders table is the hub linking customers to products.
  • Compute derived values like line totals in the SELECT after the joins have paired the rows.

An order links a customer to a product. Produce a readable line item for each order.

Schema

CREATE TABLE customers (
    id   INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
);

CREATE TABLE products (
    id           INT PRIMARY KEY,
    product_name VARCHAR(50) NOT NULL,
    price        NUMERIC(10,2) NOT NULL
);

CREATE TABLE orders (
    id          INT PRIMARY KEY,
    customer_id INT REFERENCES customers(id),
    product_id  INT REFERENCES products(id),
    quantity    INT NOT NULL
);

customers

idname
1Alice
2Bob
3Carol

products

idproduct_nameprice
100Keyboard25.00
200Mouse15.00
300Monitor150.00

orders

idcustomer_idproduct_idquantity
111002
212001
323001
431003

Task

Return one row per order with columns customer_name, product_name, quantity, and line_total (equal to quantity * price). Order by customer_name ascending, then product_name ascending.

Expected output

customer_nameproduct_namequantityline_total
AliceKeyboard250.00
AliceMouse115.00
BobMonitor1150.00
CarolKeyboard375.00
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.