Case 5: Basket analysis with a self join
People who buy running shoes often add socks. Pairs like that are worth money: as a bundle, as a recommendation on the product page or as the second product in an email. A self join on the order lines finds them for you.
In short: join order_items a to order_items b on the same order_id. With a.product_id < b.product_id each pair appears exactly once. When one side is fixed (a.product_id = 1), use <> and divide by the number of orders containing that product.
The situation
The e-commerce team wants two features: bundles with a small discount and a "frequently bought together" box on product pages. Before anyone builds guesses into the store, the orders should show which products people really buy together.
The data
order_items has one row per order line: order_id, product_id, quantity and unit_price. A product appears at most once per order, which the primary key on order_id and product_id guarantees. Names are in products. First, how many orders contain more than one product at all?
WITH per_order AS (
SELECT order_id, COUNT(*) AS n
FROM order_items
GROUP BY order_id
)
SELECT n AS lines, COUNT(*) AS orders
FROM per_order
GROUP BY n;
| lines | orders |
|---|---|
| 1 | 189 |
| 2 | 42 |
| 3 | 8 |
Only 50 of the 239 orders have more than one product, and every pair comes from those 50 baskets. That is normal for a small store and a reason to read results based on a handful of orders with care.
Task 1: frequent product pairs
Task Which products often end up in the basket together? Show every product pair that appears together in at least 3 orders: product_1 and product_2 (the product names, the one with the lower product_id first) and orders (the number of orders containing the pair). Use all order items.
order_items 297 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 5001 | 21 | 1 | 19.9 |
| 5001 | 22 | 1 | 9.9 |
| 5002 | 1 | 1 | 89.9 |
| 5002 | 3 | 1 | 24.9 |
| 5002 | 9 | 1 | 24.9 |
| 5003 | 7 | 1 | 49.9 |
| 5004 | 7 | 1 | 49.9 |
| 5004 | 8 | 1 | 19.9 |
| 5005 | 1 | 1 | 89.9 |
| 5006 | 3 | 1 | 24.9 |
| 5007 | 9 | 1 | 24.9 |
| 5008 | 1 | 1 | 89.9 |
| 5008 | 2 | 1 | 14.9 |
| 5008 | 5 | 1 | 34.9 |
| 5009 | 5 | 1 | 34.9 |
| 5010 | 3 | 1 | 24.9 |
| 5011 | 21 | 1 | 19.9 |
| 5012 | 7 | 1 | 49.9 |
| 5013 | 1 | 1 | 89.9 |
| 5014 | 9 | 1 | 24.9 |
| 5015 | 9 | 1 | 24.9 |
| 5016 | 13 | 1 | 79.9 |
| 5017 | 5 | 1 | 34.9 |
| 5018 | 1 | 1 | 89.9 |
| 5019 | 1 | 1 | 89.9 |
| 5019 | 3 | 1 | 24.9 |
| 5020 | 16 | 1 | 99.9 |
| 5021 | 13 | 1 | 79.9 |
| 5022 | 19 | 1 | 29.9 |
| 5023 | 5 | 1 | 34.9 |
| 5024 | 1 | 1 | 89.9 |
| 5025 | 8 | 1 | 19.9 |
| 5026 | 23 | 1 | 39.9 |
| 5027 | 13 | 1 | 79.9 |
| 5028 | 1 | 1 | 89.9 |
| 5029 | 21 | 1 | 19.9 |
| 5029 | 22 | 1 | 9.9 |
| 5030 | 15 | 1 | 27.9 |
| 5031 | 8 | 1 | 19.9 |
| 5032 | 13 | 1 | 79.9 |
| 5033 | 5 | 1 | 34.9 |
| 5034 | 9 | 1 | 24.9 |
| 5035 | 2 | 2 | 14.9 |
| 5036 | 21 | 1 | 19.9 |
| 5037 | 18 | 2 | 9.9 |
| 5038 | 3 | 1 | 24.9 |
| 5039 | 1 | 1 | 89.9 |
| 5039 | 2 | 2 | 14.9 |
| 5040 | 7 | 1 | 49.9 |
| 5041 | 5 | 1 | 34.9 |
| 5042 | 1 | 1 | 89.9 |
| 5043 | 5 | 1 | 34.9 |
| 5044 | 1 | 1 | 89.9 |
| 5044 | 2 | 1 | 14.9 |
| 5045 | 11 | 1 | 29.9 |
| 5046 | 17 | 1 | 119 |
| 5047 | 1 | 1 | 89.9 |
| 5048 | 18 | 1 | 9.9 |
| 5049 | 21 | 1 | 19.9 |
| 5050 | 1 | 1 | 89.9 |
| 5051 | 1 | 1 | 89.9 |
| 5052 | 4 | 1 | 12.9 |
| 5053 | 9 | 1 | 24.9 |
| 5054 | 5 | 1 | 34.9 |
| 5055 | 16 | 1 | 99.9 |
| 5056 | 4 | 2 | 12.9 |
| 5057 | 8 | 1 | 19.9 |
| 5058 | 16 | 1 | 99.9 |
| 5059 | 5 | 1 | 34.9 |
| 5060 | 22 | 1 | 9.9 |
| 5061 | 3 | 1 | 24.9 |
| 5062 | 13 | 1 | 79.9 |
| 5063 | 11 | 1 | 29.9 |
| 5064 | 24 | 1 | 129 |
| 5065 | 7 | 1 | 49.9 |
| 5065 | 8 | 1 | 19.9 |
| 5065 | 14 | 1 | 16.9 |
| 5066 | 1 | 1 | 89.9 |
| 5066 | 2 | 2 | 14.9 |
| 5067 | 16 | 1 | 99.9 |
| 5068 | 1 | 1 | 89.9 |
| 5068 | 2 | 2 | 14.9 |
| 5069 | 5 | 1 | 34.9 |
| 5069 | 8 | 1 | 19.9 |
| 5070 | 7 | 1 | 49.9 |
| 5071 | 14 | 1 | 16.9 |
| 5072 | 1 | 1 | 89.9 |
| 5073 | 5 | 1 | 34.9 |
| 5073 | 8 | 1 | 19.9 |
| 5074 | 16 | 1 | 99.9 |
| 5075 | 5 | 1 | 34.9 |
| 5076 | 1 | 1 | 89.9 |
| 5076 | 3 | 1 | 24.9 |
| 5077 | 4 | 1 | 12.9 |
| 5078 | 10 | 1 | 59.9 |
| 5079 | 15 | 1 | 27.9 |
| 5080 | 21 | 1 | 19.9 |
| 5081 | 1 | 1 | 89.9 |
| 5082 | 4 | 2 | 12.9 |
| 5083 | 10 | 1 | 47.92 |
| 5084 | 1 | 1 | 71.92 |
| 5084 | 3 | 1 | 19.92 |
| 5085 | 11 | 1 | 23.92 |
| 5085 | 19 | 1 | 23.92 |
| 5085 | 20 | 1 | 7.92 |
| 5086 | 11 | 1 | 23.92 |
| 5087 | 23 | 1 | 31.92 |
| 5088 | 10 | 1 | 47.92 |
| 5089 | 11 | 1 | 23.92 |
| 5090 | 7 | 1 | 39.92 |
| 5091 | 11 | 1 | 23.92 |
| 5092 | 6 | 1 | 10.32 |
| 5093 | 1 | 1 | 71.92 |
| 5093 | 3 | 1 | 19.92 |
| 5094 | 23 | 1 | 31.92 |
| 5095 | 24 | 1 | 103.2 |
| 5096 | 13 | 1 | 63.92 |
| 5097 | 5 | 1 | 27.92 |
| 5098 | 22 | 2 | 7.92 |
| 5099 | 22 | 1 | 7.92 |
| 5100 | 5 | 1 | 27.92 |
| 5100 | 6 | 2 | 10.32 |
| 5101 | 5 | 1 | 27.92 |
| 5101 | 6 | 1 | 10.32 |
| 5102 | 23 | 1 | 31.92 |
| 5103 | 6 | 1 | 10.32 |
| 5103 | 16 | 1 | 79.92 |
| 5104 | 21 | 1 | 15.92 |
| 5105 | 22 | 2 | 9.9 |
| 5106 | 1 | 1 | 89.9 |
| 5106 | 9 | 1 | 24.9 |
| 5107 | 11 | 1 | 29.9 |
| 5108 | 7 | 1 | 49.9 |
| 5108 | 19 | 1 | 29.9 |
| 5108 | 20 | 1 | 9.9 |
| 5109 | 1 | 1 | 89.9 |
| 5109 | 2 | 1 | 14.9 |
| 5110 | 5 | 1 | 34.9 |
| 5111 | 10 | 1 | 59.9 |
| 5112 | 13 | 1 | 79.9 |
| 5113 | 14 | 1 | 16.9 |
| 5114 | 21 | 1 | 19.9 |
| 5115 | 1 | 1 | 89.9 |
| 5116 | 6 | 2 | 12.9 |
| 5117 | 1 | 1 | 89.9 |
| 5118 | 21 | 1 | 19.9 |
| 5119 | 10 | 1 | 59.9 |
| 5120 | 18 | 2 | 9.9 |
| 5121 | 17 | 1 | 119 |
| 5122 | 9 | 1 | 24.9 |
| 5123 | 13 | 1 | 79.9 |
| 5124 | 20 | 1 | 9.9 |
| 5125 | 1 | 1 | 89.9 |
| 5125 | 2 | 1 | 14.9 |
| 5126 | 7 | 1 | 49.9 |
| 5127 | 21 | 1 | 19.9 |
| 5128 | 4 | 1 | 12.9 |
| 5129 | 11 | 1 | 29.9 |
| 5130 | 10 | 1 | 59.9 |
| 5131 | 1 | 1 | 89.9 |
| 5132 | 8 | 1 | 19.9 |
| 5133 | 21 | 1 | 19.9 |
| 5133 | 22 | 1 | 9.9 |
| 5134 | 23 | 1 | 39.9 |
| 5135 | 12 | 1 | 44.9 |
| 5136 | 13 | 1 | 79.9 |
| 5137 | 13 | 1 | 79.9 |
| 5137 | 14 | 1 | 16.9 |
| 5137 | 23 | 1 | 39.9 |
| 5138 | 7 | 1 | 49.9 |
| 5139 | 5 | 1 | 34.9 |
| 5139 | 6 | 1 | 12.9 |
| 5140 | 19 | 1 | 29.9 |
| 5140 | 21 | 1 | 19.9 |
| 5141 | 8 | 1 | 19.9 |
| 5142 | 8 | 1 | 19.9 |
| 5143 | 4 | 1 | 12.9 |
| 5143 | 22 | 1 | 9.9 |
| 5144 | 1 | 1 | 89.9 |
| 5144 | 2 | 1 | 14.9 |
| 5145 | 5 | 1 | 34.9 |
| 5146 | 13 | 1 | 79.9 |
| 5146 | 15 | 1 | 27.9 |
| 5147 | 9 | 1 | 24.9 |
| 5148 | 5 | 1 | 34.9 |
| 5149 | 5 | 1 | 34.9 |
| 5150 | 4 | 2 | 12.9 |
| 5151 | 22 | 1 | 9.9 |
| 5152 | 17 | 1 | 119 |
| 5153 | 3 | 1 | 24.9 |
| 5154 | 19 | 1 | 29.9 |
| 5155 | 7 | 1 | 49.9 |
| 5156 | 1 | 1 | 89.9 |
| 5156 | 4 | 1 | 12.9 |
| 5157 | 3 | 1 | 24.9 |
| 5158 | 11 | 1 | 29.9 |
| 5159 | 13 | 1 | 79.9 |
| 5160 | 11 | 1 | 29.9 |
| 5161 | 11 | 1 | 29.9 |
| 5162 | 19 | 1 | 29.9 |
| 5163 | 12 | 1 | 44.9 |
| 5164 | 24 | 1 | 129 |
| 5165 | 4 | 2 | 12.9 |
| 5165 | 19 | 1 | 29.9 |
| 5166 | 5 | 1 | 34.9 |
| 5166 | 8 | 1 | 19.9 |
| 5167 | 2 | 1 | 14.9 |
| 5168 | 5 | 1 | 34.9 |
| 5169 | 24 | 1 | 129 |
| 5170 | 17 | 1 | 119 |
| 5170 | 18 | 3 | 9.9 |
| 5171 | 3 | 1 | 24.9 |
| 5172 | 12 | 1 | 44.9 |
| 5173 | 3 | 1 | 24.9 |
| 5173 | 24 | 1 | 129 |
| 5174 | 16 | 1 | 99.9 |
| 5175 | 24 | 1 | 129 |
| 5176 | 7 | 1 | 49.9 |
| 5177 | 21 | 1 | 19.9 |
| 5178 | 1 | 1 | 89.9 |
| 5178 | 4 | 1 | 12.9 |
| 5179 | 1 | 1 | 89.9 |
| 5179 | 2 | 2 | 14.9 |
| 5180 | 18 | 3 | 9.9 |
| 5181 | 4 | 1 | 12.9 |
| 5182 | 16 | 1 | 99.9 |
| 5183 | 24 | 1 | 129 |
| 5184 | 3 | 1 | 24.9 |
| 5185 | 16 | 1 | 99.9 |
| 5186 | 17 | 1 | 119 |
| 5186 | 23 | 1 | 39.9 |
| 5187 | 7 | 1 | 49.9 |
| 5187 | 8 | 1 | 19.9 |
| 5187 | 12 | 1 | 44.9 |
| 5188 | 23 | 1 | 39.9 |
| 5189 | 21 | 1 | 19.9 |
| 5190 | 7 | 1 | 49.9 |
| 5191 | 17 | 1 | 119 |
| 5191 | 18 | 3 | 9.9 |
| 5192 | 5 | 1 | 34.9 |
| 5193 | 5 | 1 | 34.9 |
| 5194 | 13 | 1 | 79.9 |
| 5195 | 14 | 2 | 16.9 |
| 5196 | 8 | 1 | 19.9 |
| 5197 | 15 | 1 | 27.9 |
| 5198 | 16 | 1 | 99.9 |
| 5199 | 19 | 1 | 29.9 |
| 5200 | 17 | 1 | 119 |
| 5201 | 1 | 1 | 89.9 |
| 5201 | 4 | 1 | 12.9 |
| 5202 | 1 | 1 | 89.9 |
| 5203 | 5 | 1 | 34.9 |
| 5203 | 6 | 1 | 12.9 |
| 5204 | 15 | 1 | 27.9 |
| 5205 | 12 | 1 | 44.9 |
| 5206 | 10 | 1 | 59.9 |
| 5206 | 11 | 1 | 29.9 |
| 5207 | 1 | 1 | 89.9 |
| 5207 | 4 | 1 | 12.9 |
| 5207 | 23 | 1 | 39.9 |
| 5208 | 13 | 1 | 79.9 |
| 5208 | 14 | 2 | 16.9 |
| 5209 | 16 | 1 | 99.9 |
| 5210 | 2 | 2 | 14.9 |
| 5211 | 21 | 1 | 19.9 |
| 5212 | 2 | 2 | 14.9 |
| 5212 | 6 | 1 | 12.9 |
| 5213 | 19 | 1 | 29.9 |
| 5214 | 2 | 2 | 14.9 |
| 5215 | 3 | 1 | 24.9 |
| 5216 | 1 | 1 | 89.9 |
| 5217 | 21 | 1 | 19.9 |
| 5218 | 13 | 1 | 79.9 |
| 5218 | 14 | 1 | 16.9 |
| 5219 | 1 | 1 | 89.9 |
| 5220 | 12 | 1 | 44.9 |
| 5221 | 1 | 1 | 89.9 |
| 5222 | 1 | 1 | 89.9 |
| 5223 | 1 | 1 | 89.9 |
| 5224 | 4 | 2 | 12.9 |
| 5225 | 7 | 1 | 49.9 |
| 5226 | 3 | 1 | 24.9 |
| 5227 | 15 | 1 | 27.9 |
| 5228 | 19 | 1 | 29.9 |
| 5229 | 9 | 1 | 24.9 |
| 5230 | 9 | 1 | 24.9 |
| 5231 | 9 | 1 | 24.9 |
| 5232 | 7 | 1 | 49.9 |
| 5233 | 2 | 1 | 14.9 |
| 5234 | 3 | 1 | 24.9 |
| 5235 | 5 | 1 | 34.9 |
| 5236 | 5 | 1 | 34.9 |
| 5237 | 10 | 1 | 59.9 |
| 5237 | 11 | 1 | 29.9 |
| 5238 | 8 | 1 | 19.9 |
| 5239 | 1 | 1 | 89.9 |
| 5239 | 2 | 1 | 14.9 |
products 24 rows, 5 columns
| product_id | name | category | price | unit_cost |
|---|---|---|---|---|
| 1 | Running shoes | Running | 89.9 | 48 |
| 2 | Running socks, 3 pairs | Running | 14.9 | 5.2 |
| 3 | Running shirt | Running | 24.9 | 10.5 |
| 4 | Water bottle 750 ml | Accessories | 12.9 | 4.1 |
| 5 | Yoga mat | Fitness | 34.9 | 14 |
| 6 | Cork yoga block | Fitness | 12.9 | 4.8 |
| 7 | Dumbbells, pair | Fitness | 49.9 | 22 |
| 8 | Resistance bands, set of 3 | Fitness | 19.9 | 6 |
| 9 | Foam roller | Fitness | 24.9 | 9 |
| 10 | Bike helmet | Cycling | 59.9 | 27 |
| 11 | Bike light set | Cycling | 29.9 | 11.5 |
| 12 | Cycling jersey | Cycling | 44.9 | 19 |
| 13 | Hiking backpack 30 l | Outdoor | 79.9 | 36 |
| 14 | Hiking socks | Outdoor | 16.9 | 6.1 |
| 15 | Headlamp | Outdoor | 27.9 | 10.8 |
| 16 | Rain jacket | Outdoor | 99.9 | 46 |
| 17 | Tennis racket | Ball sports | 119 | 58 |
| 18 | Tennis balls, can of 4 | Ball sports | 9.9 | 3.4 |
| 19 | Training ball, size 5 | Ball sports | 29.9 | 11 |
| 20 | Ball pump | Ball sports | 9.9 | 3 |
| 21 | Swim goggles | Swimming | 19.9 | 6.5 |
| 22 | Swim cap | Swimming | 9.9 | 2.6 |
| 23 | Sports bag | Accessories | 39.9 | 15 |
| 24 | Heart rate watch | Accessories | 129 | 72 |
How it works
In a self join, the same table enters the query twice under different aliases. The condition b.order_id = a.order_id pairs every line with every line of the same order, including itself and in both directions. An order with running shoes and socks therefore produces four rows: shoes with shoes, shoes with socks, socks with shoes, socks with socks.
a.product_id < b.product_id keeps exactly one of them: shoes (1) with socks (2). An order with three products leaves three pairs. Then you group by both product IDs, count and use HAVING to keep pairs with at least three orders. The names come from two joins to products, one for each side.
Task 2: "frequently bought together"
Task The product page for the running shoes (product_id 1) is getting a "Frequently bought together" box. For every other product that was in the same order as the running shoes, show the name, the number of orders containing both, and the share as a percentage of all orders with running shoes (one decimal place).
order_items 297 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 5001 | 21 | 1 | 19.9 |
| 5001 | 22 | 1 | 9.9 |
| 5002 | 1 | 1 | 89.9 |
| 5002 | 3 | 1 | 24.9 |
| 5002 | 9 | 1 | 24.9 |
| 5003 | 7 | 1 | 49.9 |
| 5004 | 7 | 1 | 49.9 |
| 5004 | 8 | 1 | 19.9 |
| 5005 | 1 | 1 | 89.9 |
| 5006 | 3 | 1 | 24.9 |
| 5007 | 9 | 1 | 24.9 |
| 5008 | 1 | 1 | 89.9 |
| 5008 | 2 | 1 | 14.9 |
| 5008 | 5 | 1 | 34.9 |
| 5009 | 5 | 1 | 34.9 |
| 5010 | 3 | 1 | 24.9 |
| 5011 | 21 | 1 | 19.9 |
| 5012 | 7 | 1 | 49.9 |
| 5013 | 1 | 1 | 89.9 |
| 5014 | 9 | 1 | 24.9 |
| 5015 | 9 | 1 | 24.9 |
| 5016 | 13 | 1 | 79.9 |
| 5017 | 5 | 1 | 34.9 |
| 5018 | 1 | 1 | 89.9 |
| 5019 | 1 | 1 | 89.9 |
| 5019 | 3 | 1 | 24.9 |
| 5020 | 16 | 1 | 99.9 |
| 5021 | 13 | 1 | 79.9 |
| 5022 | 19 | 1 | 29.9 |
| 5023 | 5 | 1 | 34.9 |
| 5024 | 1 | 1 | 89.9 |
| 5025 | 8 | 1 | 19.9 |
| 5026 | 23 | 1 | 39.9 |
| 5027 | 13 | 1 | 79.9 |
| 5028 | 1 | 1 | 89.9 |
| 5029 | 21 | 1 | 19.9 |
| 5029 | 22 | 1 | 9.9 |
| 5030 | 15 | 1 | 27.9 |
| 5031 | 8 | 1 | 19.9 |
| 5032 | 13 | 1 | 79.9 |
| 5033 | 5 | 1 | 34.9 |
| 5034 | 9 | 1 | 24.9 |
| 5035 | 2 | 2 | 14.9 |
| 5036 | 21 | 1 | 19.9 |
| 5037 | 18 | 2 | 9.9 |
| 5038 | 3 | 1 | 24.9 |
| 5039 | 1 | 1 | 89.9 |
| 5039 | 2 | 2 | 14.9 |
| 5040 | 7 | 1 | 49.9 |
| 5041 | 5 | 1 | 34.9 |
| 5042 | 1 | 1 | 89.9 |
| 5043 | 5 | 1 | 34.9 |
| 5044 | 1 | 1 | 89.9 |
| 5044 | 2 | 1 | 14.9 |
| 5045 | 11 | 1 | 29.9 |
| 5046 | 17 | 1 | 119 |
| 5047 | 1 | 1 | 89.9 |
| 5048 | 18 | 1 | 9.9 |
| 5049 | 21 | 1 | 19.9 |
| 5050 | 1 | 1 | 89.9 |
| 5051 | 1 | 1 | 89.9 |
| 5052 | 4 | 1 | 12.9 |
| 5053 | 9 | 1 | 24.9 |
| 5054 | 5 | 1 | 34.9 |
| 5055 | 16 | 1 | 99.9 |
| 5056 | 4 | 2 | 12.9 |
| 5057 | 8 | 1 | 19.9 |
| 5058 | 16 | 1 | 99.9 |
| 5059 | 5 | 1 | 34.9 |
| 5060 | 22 | 1 | 9.9 |
| 5061 | 3 | 1 | 24.9 |
| 5062 | 13 | 1 | 79.9 |
| 5063 | 11 | 1 | 29.9 |
| 5064 | 24 | 1 | 129 |
| 5065 | 7 | 1 | 49.9 |
| 5065 | 8 | 1 | 19.9 |
| 5065 | 14 | 1 | 16.9 |
| 5066 | 1 | 1 | 89.9 |
| 5066 | 2 | 2 | 14.9 |
| 5067 | 16 | 1 | 99.9 |
| 5068 | 1 | 1 | 89.9 |
| 5068 | 2 | 2 | 14.9 |
| 5069 | 5 | 1 | 34.9 |
| 5069 | 8 | 1 | 19.9 |
| 5070 | 7 | 1 | 49.9 |
| 5071 | 14 | 1 | 16.9 |
| 5072 | 1 | 1 | 89.9 |
| 5073 | 5 | 1 | 34.9 |
| 5073 | 8 | 1 | 19.9 |
| 5074 | 16 | 1 | 99.9 |
| 5075 | 5 | 1 | 34.9 |
| 5076 | 1 | 1 | 89.9 |
| 5076 | 3 | 1 | 24.9 |
| 5077 | 4 | 1 | 12.9 |
| 5078 | 10 | 1 | 59.9 |
| 5079 | 15 | 1 | 27.9 |
| 5080 | 21 | 1 | 19.9 |
| 5081 | 1 | 1 | 89.9 |
| 5082 | 4 | 2 | 12.9 |
| 5083 | 10 | 1 | 47.92 |
| 5084 | 1 | 1 | 71.92 |
| 5084 | 3 | 1 | 19.92 |
| 5085 | 11 | 1 | 23.92 |
| 5085 | 19 | 1 | 23.92 |
| 5085 | 20 | 1 | 7.92 |
| 5086 | 11 | 1 | 23.92 |
| 5087 | 23 | 1 | 31.92 |
| 5088 | 10 | 1 | 47.92 |
| 5089 | 11 | 1 | 23.92 |
| 5090 | 7 | 1 | 39.92 |
| 5091 | 11 | 1 | 23.92 |
| 5092 | 6 | 1 | 10.32 |
| 5093 | 1 | 1 | 71.92 |
| 5093 | 3 | 1 | 19.92 |
| 5094 | 23 | 1 | 31.92 |
| 5095 | 24 | 1 | 103.2 |
| 5096 | 13 | 1 | 63.92 |
| 5097 | 5 | 1 | 27.92 |
| 5098 | 22 | 2 | 7.92 |
| 5099 | 22 | 1 | 7.92 |
| 5100 | 5 | 1 | 27.92 |
| 5100 | 6 | 2 | 10.32 |
| 5101 | 5 | 1 | 27.92 |
| 5101 | 6 | 1 | 10.32 |
| 5102 | 23 | 1 | 31.92 |
| 5103 | 6 | 1 | 10.32 |
| 5103 | 16 | 1 | 79.92 |
| 5104 | 21 | 1 | 15.92 |
| 5105 | 22 | 2 | 9.9 |
| 5106 | 1 | 1 | 89.9 |
| 5106 | 9 | 1 | 24.9 |
| 5107 | 11 | 1 | 29.9 |
| 5108 | 7 | 1 | 49.9 |
| 5108 | 19 | 1 | 29.9 |
| 5108 | 20 | 1 | 9.9 |
| 5109 | 1 | 1 | 89.9 |
| 5109 | 2 | 1 | 14.9 |
| 5110 | 5 | 1 | 34.9 |
| 5111 | 10 | 1 | 59.9 |
| 5112 | 13 | 1 | 79.9 |
| 5113 | 14 | 1 | 16.9 |
| 5114 | 21 | 1 | 19.9 |
| 5115 | 1 | 1 | 89.9 |
| 5116 | 6 | 2 | 12.9 |
| 5117 | 1 | 1 | 89.9 |
| 5118 | 21 | 1 | 19.9 |
| 5119 | 10 | 1 | 59.9 |
| 5120 | 18 | 2 | 9.9 |
| 5121 | 17 | 1 | 119 |
| 5122 | 9 | 1 | 24.9 |
| 5123 | 13 | 1 | 79.9 |
| 5124 | 20 | 1 | 9.9 |
| 5125 | 1 | 1 | 89.9 |
| 5125 | 2 | 1 | 14.9 |
| 5126 | 7 | 1 | 49.9 |
| 5127 | 21 | 1 | 19.9 |
| 5128 | 4 | 1 | 12.9 |
| 5129 | 11 | 1 | 29.9 |
| 5130 | 10 | 1 | 59.9 |
| 5131 | 1 | 1 | 89.9 |
| 5132 | 8 | 1 | 19.9 |
| 5133 | 21 | 1 | 19.9 |
| 5133 | 22 | 1 | 9.9 |
| 5134 | 23 | 1 | 39.9 |
| 5135 | 12 | 1 | 44.9 |
| 5136 | 13 | 1 | 79.9 |
| 5137 | 13 | 1 | 79.9 |
| 5137 | 14 | 1 | 16.9 |
| 5137 | 23 | 1 | 39.9 |
| 5138 | 7 | 1 | 49.9 |
| 5139 | 5 | 1 | 34.9 |
| 5139 | 6 | 1 | 12.9 |
| 5140 | 19 | 1 | 29.9 |
| 5140 | 21 | 1 | 19.9 |
| 5141 | 8 | 1 | 19.9 |
| 5142 | 8 | 1 | 19.9 |
| 5143 | 4 | 1 | 12.9 |
| 5143 | 22 | 1 | 9.9 |
| 5144 | 1 | 1 | 89.9 |
| 5144 | 2 | 1 | 14.9 |
| 5145 | 5 | 1 | 34.9 |
| 5146 | 13 | 1 | 79.9 |
| 5146 | 15 | 1 | 27.9 |
| 5147 | 9 | 1 | 24.9 |
| 5148 | 5 | 1 | 34.9 |
| 5149 | 5 | 1 | 34.9 |
| 5150 | 4 | 2 | 12.9 |
| 5151 | 22 | 1 | 9.9 |
| 5152 | 17 | 1 | 119 |
| 5153 | 3 | 1 | 24.9 |
| 5154 | 19 | 1 | 29.9 |
| 5155 | 7 | 1 | 49.9 |
| 5156 | 1 | 1 | 89.9 |
| 5156 | 4 | 1 | 12.9 |
| 5157 | 3 | 1 | 24.9 |
| 5158 | 11 | 1 | 29.9 |
| 5159 | 13 | 1 | 79.9 |
| 5160 | 11 | 1 | 29.9 |
| 5161 | 11 | 1 | 29.9 |
| 5162 | 19 | 1 | 29.9 |
| 5163 | 12 | 1 | 44.9 |
| 5164 | 24 | 1 | 129 |
| 5165 | 4 | 2 | 12.9 |
| 5165 | 19 | 1 | 29.9 |
| 5166 | 5 | 1 | 34.9 |
| 5166 | 8 | 1 | 19.9 |
| 5167 | 2 | 1 | 14.9 |
| 5168 | 5 | 1 | 34.9 |
| 5169 | 24 | 1 | 129 |
| 5170 | 17 | 1 | 119 |
| 5170 | 18 | 3 | 9.9 |
| 5171 | 3 | 1 | 24.9 |
| 5172 | 12 | 1 | 44.9 |
| 5173 | 3 | 1 | 24.9 |
| 5173 | 24 | 1 | 129 |
| 5174 | 16 | 1 | 99.9 |
| 5175 | 24 | 1 | 129 |
| 5176 | 7 | 1 | 49.9 |
| 5177 | 21 | 1 | 19.9 |
| 5178 | 1 | 1 | 89.9 |
| 5178 | 4 | 1 | 12.9 |
| 5179 | 1 | 1 | 89.9 |
| 5179 | 2 | 2 | 14.9 |
| 5180 | 18 | 3 | 9.9 |
| 5181 | 4 | 1 | 12.9 |
| 5182 | 16 | 1 | 99.9 |
| 5183 | 24 | 1 | 129 |
| 5184 | 3 | 1 | 24.9 |
| 5185 | 16 | 1 | 99.9 |
| 5186 | 17 | 1 | 119 |
| 5186 | 23 | 1 | 39.9 |
| 5187 | 7 | 1 | 49.9 |
| 5187 | 8 | 1 | 19.9 |
| 5187 | 12 | 1 | 44.9 |
| 5188 | 23 | 1 | 39.9 |
| 5189 | 21 | 1 | 19.9 |
| 5190 | 7 | 1 | 49.9 |
| 5191 | 17 | 1 | 119 |
| 5191 | 18 | 3 | 9.9 |
| 5192 | 5 | 1 | 34.9 |
| 5193 | 5 | 1 | 34.9 |
| 5194 | 13 | 1 | 79.9 |
| 5195 | 14 | 2 | 16.9 |
| 5196 | 8 | 1 | 19.9 |
| 5197 | 15 | 1 | 27.9 |
| 5198 | 16 | 1 | 99.9 |
| 5199 | 19 | 1 | 29.9 |
| 5200 | 17 | 1 | 119 |
| 5201 | 1 | 1 | 89.9 |
| 5201 | 4 | 1 | 12.9 |
| 5202 | 1 | 1 | 89.9 |
| 5203 | 5 | 1 | 34.9 |
| 5203 | 6 | 1 | 12.9 |
| 5204 | 15 | 1 | 27.9 |
| 5205 | 12 | 1 | 44.9 |
| 5206 | 10 | 1 | 59.9 |
| 5206 | 11 | 1 | 29.9 |
| 5207 | 1 | 1 | 89.9 |
| 5207 | 4 | 1 | 12.9 |
| 5207 | 23 | 1 | 39.9 |
| 5208 | 13 | 1 | 79.9 |
| 5208 | 14 | 2 | 16.9 |
| 5209 | 16 | 1 | 99.9 |
| 5210 | 2 | 2 | 14.9 |
| 5211 | 21 | 1 | 19.9 |
| 5212 | 2 | 2 | 14.9 |
| 5212 | 6 | 1 | 12.9 |
| 5213 | 19 | 1 | 29.9 |
| 5214 | 2 | 2 | 14.9 |
| 5215 | 3 | 1 | 24.9 |
| 5216 | 1 | 1 | 89.9 |
| 5217 | 21 | 1 | 19.9 |
| 5218 | 13 | 1 | 79.9 |
| 5218 | 14 | 1 | 16.9 |
| 5219 | 1 | 1 | 89.9 |
| 5220 | 12 | 1 | 44.9 |
| 5221 | 1 | 1 | 89.9 |
| 5222 | 1 | 1 | 89.9 |
| 5223 | 1 | 1 | 89.9 |
| 5224 | 4 | 2 | 12.9 |
| 5225 | 7 | 1 | 49.9 |
| 5226 | 3 | 1 | 24.9 |
| 5227 | 15 | 1 | 27.9 |
| 5228 | 19 | 1 | 29.9 |
| 5229 | 9 | 1 | 24.9 |
| 5230 | 9 | 1 | 24.9 |
| 5231 | 9 | 1 | 24.9 |
| 5232 | 7 | 1 | 49.9 |
| 5233 | 2 | 1 | 14.9 |
| 5234 | 3 | 1 | 24.9 |
| 5235 | 5 | 1 | 34.9 |
| 5236 | 5 | 1 | 34.9 |
| 5237 | 10 | 1 | 59.9 |
| 5237 | 11 | 1 | 29.9 |
| 5238 | 8 | 1 | 19.9 |
| 5239 | 1 | 1 | 89.9 |
| 5239 | 2 | 1 | 14.9 |
products 24 rows, 5 columns
| product_id | name | category | price | unit_cost |
|---|---|---|---|---|
| 1 | Running shoes | Running | 89.9 | 48 |
| 2 | Running socks, 3 pairs | Running | 14.9 | 5.2 |
| 3 | Running shirt | Running | 24.9 | 10.5 |
| 4 | Water bottle 750 ml | Accessories | 12.9 | 4.1 |
| 5 | Yoga mat | Fitness | 34.9 | 14 |
| 6 | Cork yoga block | Fitness | 12.9 | 4.8 |
| 7 | Dumbbells, pair | Fitness | 49.9 | 22 |
| 8 | Resistance bands, set of 3 | Fitness | 19.9 | 6 |
| 9 | Foam roller | Fitness | 24.9 | 9 |
| 10 | Bike helmet | Cycling | 59.9 | 27 |
| 11 | Bike light set | Cycling | 29.9 | 11.5 |
| 12 | Cycling jersey | Cycling | 44.9 | 19 |
| 13 | Hiking backpack 30 l | Outdoor | 79.9 | 36 |
| 14 | Hiking socks | Outdoor | 16.9 | 6.1 |
| 15 | Headlamp | Outdoor | 27.9 | 10.8 |
| 16 | Rain jacket | Outdoor | 99.9 | 46 |
| 17 | Tennis racket | Ball sports | 119 | 58 |
| 18 | Tennis balls, can of 4 | Ball sports | 9.9 | 3.4 |
| 19 | Training ball, size 5 | Ball sports | 29.9 | 11 |
| 20 | Ball pump | Ball sports | 9.9 | 3 |
| 21 | Swim goggles | Swimming | 19.9 | 6.5 |
| 22 | Swim cap | Swimming | 9.9 | 2.6 |
| 23 | Sports bag | Accessories | 39.9 | 15 |
| 24 | Heart rate watch | Accessories | 129 | 72 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
How it works
Now one side is fixed: WHERE a.product_id = 1. Side b is every other product in the same orders. There is no second direction to remove, so <> is enough. For the share you need the number of orders with running shoes, which a subquery provides: (SELECT COUNT(*) FROM order_items WHERE product_id = 1). That is 40 orders, and 10 of them also contained running socks, so 25%.
In basket analysis this share is called confidence: how often is B in the basket when A is bought? Two more measures sit next to it. Support is the share of all orders that contain the pair. Lift divides confidence by B's overall share of orders. A lift above 1 means B is bought with A more often than it is bought in general.
Common mistakes
Not-equal instead of less-than for open pairs
With a.product_id <> b.product_id in task 1, every pair appears twice, once in each direction. The exercise says: "Your query returns 16 rows. Wanted: 8 rows."
Greater-than instead of less-than
With a.product_id > b.product_id the pairs are right, but the product with the higher ID comes first. The exercise reports that the columns are in the wrong order. What it means: the names in product_1 and product_2 have swapped places.
Not excluding the product itself
Without b.product_id <> a.product_id in task 2, the list recommends running shoes with running shoes at 100%: 7 rows instead of 6.
Dividing by the wrong total
Dividing by all order lines or all orders gives shares that are far too small. The question is: of the orders with running shoes, how many also contain this product?
Check your result
- Task 1: 8 pairs. Running shoes and running socks lead with 10 orders, followed by running shoes and running shirt with 5.
- Task 2: 6 products. Running socks 10 orders and 25%, running shirt 5 and 12.5%, water bottle 4 and 10%.
- Cross-check: every number in task 2 has to match the count of the same pair in task 1, wherever that pair appears there.
In MySQL, PostgreSQL and SQL Server
Self joins, subqueries and HAVING work the same everywhere. Only the division needs care: in PostgreSQL and SQL Server, 100 * COUNT(*) / (SELECT COUNT(*) …) returns a whole number because both sides are integers. 100.0 prevents that in all four systems.
FAQ
How many orders does a basket analysis need?
The more, the more reliable. With 50 baskets, as here, strong pairs stand out. Pairs that share only two or three orders could be chance, which is why task 1 keeps pairs with at least three.
Does the query get slow with lots of order lines?
The self join creates, for each order, the square of its number of lines. As long as the join runs on order_id and that column is indexed, it stays manageable even for large tables. A self join without that condition would be a cross join.
Do cancelled orders count?
In this task they do, because the question is what customers put in the basket together. To look only at goods that were actually sold, also join orders and filter on the status.