BBampaa

Lessons › SQL › Go further in SQL: Business Analytics Patterns

SQL lesson

Market basket analysis with self-join

Market basket analysis finds which items most often appear together in the same transaction by joining a table back to itself - matching each row against every other row in the same group.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Market basket - products bought together

SELECT a.product_id AS product_a,
       b.product_id AS product_b,
       COUNT(*) AS pair_count
FROM orders a
JOIN orders b
  ON a.order_id = b.order_id AND a.product_id < b.product_id
GROUP BY a.product_id, b.product_id
ORDER BY pair_count DESC

Result:

product_aproduct_bpair_count
P2001P20022
P2002P20031

Step by step

  1. Run the query without COUNT so you can see what the unfiltered values look like.
    SELECT * FROM orders a JOIN orders b ON a.order_id = b.order_id AND a.product_id < b.product_id;
  2. COUNT tells you how many rows match - perfect for sizing a table.
    SELECT a.product_id AS product_a, b.product_id AS product_b, COUNT(*) FROM orders a JOIN orders b ON a.order_id = b.order_id AND a.product_id < b.product_id GROUP BY a.product_id, b.product_id ORDER BY COUNT(*) DESC;
  3. AS gives the COUNT result a friendly column name so the output reads cleanly.
    SELECT a.product_id AS product_a, b.product_id AS product_b, COUNT(*) AS pair_count FROM orders a JOIN orders b ON a.order_id = b.order_id AND a.product_id < b.product_id GROUP BY a.product_id, b.product_id ORDER BY pair_count DESC;

Practice

Try each one in your head first, then open the answer.

1. Using order_items, list each pair of products that were bought in the same order, each pair once (use a.product < b.product). Show product_a and product_b.
SELECT DISTINCT a.product AS product_a, b.product AS product_b FROM order_items a JOIN order_items b ON a.order_id = b.order_id AND a.product < b.product ORDER BY product_a, product_b
2. Using order_items, count how many orders each pair of products appears in, most common first, then A to Z. Show product_a, product_b and orders.
SELECT a.product AS product_a, b.product AS product_b, COUNT(*) AS orders FROM order_items a JOIN order_items b ON a.order_id = b.order_id AND a.product < b.product GROUP BY a.product, b.product ORDER BY orders DESC, product_a, product_b
3. Using cart_items, count how many carts each pair of products appears in, most common first, then A to Z. Show product_a, product_b and carts.
SELECT a.product AS product_a, b.product AS product_b, COUNT(*) AS carts FROM cart_items a JOIN cart_items b ON a.cart_id = b.cart_id AND a.product < b.product GROUP BY a.product, b.product ORDER BY carts DESC, product_a, product_b
4. Using order_items, which product is most often bought together with Bread? Show product and orders for the top one.
SELECT b.product, COUNT(*) AS orders FROM order_items a JOIN order_items b ON a.order_id = b.order_id AND a.product <> b.product WHERE a.product = 'Bread' GROUP BY b.product ORDER BY orders DESC LIMIT 1
5. Using order_items, show the pairs of products bought together in at least 2 orders, most common first. Show product_a, product_b and orders.
SELECT a.product AS product_a, b.product AS product_b, COUNT(*) AS orders FROM order_items a JOIN order_items b ON a.order_id = b.order_id AND a.product < b.product GROUP BY a.product, b.product HAVING COUNT(*) >= 2 ORDER BY orders DESC

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed