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 →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| product_a | product_b | pair_count |
|---|---|---|
| P2001 | P2002 | 2 |
| P2002 | P2003 | 1 |
Step by step
- 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; - 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; - 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
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_b2. 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_b3. 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_b4. 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 15. 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 DESCLearn 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 →