← Back to SQL, page 22

sql-0424 · Medium mid

Which pairs of products are most often bought in the same order?

Self-join order_items on the same order with a.product < b.product, so each unordered pair appears once and a product never pairs with itself, then count orders per pair and sort by the count.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
SELECT a.product AS product_a, b.product AS product_b, COUNT(DISTINCT a.order_id) AS orders_together
FROM order_items a JOIN order_items b ON b.order_id = a.order_id AND a.product < b.product
GROUP BY a.product, b.product ORDER BY orders_together DESC, product_a, product_b;
product_aproduct_borders_together
BagNotebook1
NotebookPen1

Order 101 contains Notebook and Pen, and order 104 contains Bag and Notebook, so each of those pairs appears once. The < condition is the important part: <> would count each pair twice (A,B and B,A), and no condition at all would pair every product with itself. COUNT(DISTINCT order_id) protects against an order listing the same product on two lines. On a large table the self-join grows quickly with basket size, so production analyses often restrict to recent orders or use a sampling approach.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

SELECT a.product AS product_a, b.product AS product_b, COUNT(DISTINCT a.order_id) AS orders_together
FROM order_items a JOIN order_items b ON b.order_id = a.order_id AND a.product < b.product
GROUP BY a.product, b.product ORDER BY orders_together DESC, product_a, product_b;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Self-join within the same order
  • a < b gives each unordered pair once
  • Count distinct orders per pair
  • Self-joins grow with basket size

Common mistakes

  • Using <> and double counting
  • Counting rows instead of orders

Follow-ups

  • How would you compute support and confidence?
  • How would you find triples?

Tips

  • Explain the < condition