EasyJOINs (INNER, LEFT, RIGHT, FULL)

Get order items with product names and order info

The query

SQL
SELECT
  o.order_id,
  c.name AS customer_name,
  p.name AS product_name,
  oi.qty,
  oi.unit_price,
  (oi.qty * oi.unit_price) AS line_total
FROM order_items oi
JOIN orders o ON oi.order_id = o.order_id
JOIN customers c ON o.cust_id = c.cust_id
JOIN products p ON oi.product_id = p.product_id;
Tested against PostgreSQL 16

Note

3-table JOIN chains: each JOIN adds another table. Match foreign keys correctly.

Tables referenced