IntermediateJOINs (INNER, LEFT, RIGHT, FULL)

4-table JOIN: order details with all info

The query

SQL
SELECT
  o.order_id,
  cu.name AS customer,
  cu.city,
  p.name AS product,
  cat.name AS category,
  oi.qty,
  oi.unit_price,
  (oi.qty * oi.unit_price * (1 - oi.discount/100)) AS net_amount
FROM orders o
JOIN customers cu  ON o.cust_id = cu.cust_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p    ON oi.product_id = p.product_id
JOIN categories cat ON p.cat_id = cat.cat_id
WHERE o.status = 'completed'
ORDER BY o.order_date DESC;
Tested against PostgreSQL 16

Note

Multi-table joins are very common in real interviews. Always trace the FK relationships.

Tables referenced