Basket analysis and allocation · Original fictional data · Free files and full answers
Compare basket similarity using SKU sets
Compare every pair of orders a<b. Return order_a,order_b,intersection_count,union_count for pairs sharing at least one SKU. Quantities are ignored. Sort order_a,order_b.
Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.
Your output contract
- Compare every pair of orders a<b. Return order_a,order_b,intersection_count,union_count for pairs sharing at least one SKU. Quantities are ignored. Sort order_a,order_b.
- Return exactly the declared columns and order. Money uses integer cents; all time values are elapsed UTC minutes unless a field is explicitly a day index. NULL is unknown and differs from numeric zero.
- These are original fictional records and exercise policies. All source tables in this case are included in the downloadable pack.
Return columns exactly as order_a, order_b, intersection_count, union_count. Sort order follows the task. Output has 18 rows; an empty CSV field means NULL.
Source data
basket
18 source rows · Download basket CSV
| order_id INTEGER | sku TEXT | qty INTEGER | price INTEGER | category TEXT |
|---|---|---|---|---|
| 1 | A | 2 | 500 | book |
| 1 | B | 1 | 1200 | tool |
| 1 | C | 1 | 500 | book |
| 2 | A | 1 | 500 | book |
| 2 | D | 3 | 300 | food |
| 3 | B | 2 | 1200 | tool |
| 3 | E | 1 | 800 | food |
| 4 | C | 3 | 500 | book |
| 4 | D | 1 | 300 | food |
| 5 | A | 1 | 500 | book |
| 5 | B | 1 | 1200 | tool |
| 5 | E | 2 | 800 | food |
| 6 | F | 1 | 0 | gift |
| 6 | D | 2 | 300 | food |
| 7 | C | 1 | 500 | book |
| 7 | E | 1 | 800 | food |
| 8 | A | 1 | 500 | book |
| 8 | C | 2 | 500 | book |
required
2 source rows · Download required CSV
| sku TEXT |
|---|
| A |
| B |
Field dictionary and schema
basket: order_id INTEGER — Stable record or business key; retain its declared type. sku TEXT — Literal text label or key; comparisons use SQLite binary text semantics. qty INTEGER — Exact integer measurement; use the task rule for units and ordering. price INTEGER — Exact integer measurement; use the task rule for units and ordering. category TEXT — Literal text label or key; comparisons use SQLite binary text semantics. required: sku TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
A method you can reuse
- Compare every pair of orders a<b. Return order_a,order_b,intersection_count,union_count for pairs sharing at least one SKU. Quantities are ignored. Sort order_a,order_b.
- Inspect the source grain, keys and ties before writing the query. Use the worked SQL and complete expected CSV to review any mismatch.
Watch for this wrong turn
Joining raw lines and counting matches measures quantity/row multiplicity rather than set intersection.
Run and check in SQLite
Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.
Complete worked SQL and expected output
WITH o AS (SELECT DISTINCT order_id FROM basket), p AS (SELECT a.order_id a,b.order_id b FROM o a JOIN o b ON a.order_id<b.order_id) SELECT a order_a,b order_b,(SELECT COUNT(*) FROM (SELECT sku FROM basket WHERE order_id=p.a INTERSECT SELECT sku FROM basket WHERE order_id=p.b)) intersection_count,(SELECT COUNT(*) FROM (SELECT sku FROM basket WHERE order_id=p.a UNION SELECT sku FROM basket WHERE order_id=p.b)) union_count FROM p WHERE intersection_count>0 ORDER BY a,b;
"order_a","order_b","intersection_count","union_count" "1","2","1","4" "1","3","1","4" "1","4","1","4" "1","5","2","4" "1","7","1","4" "1","8","2","3" "2","4","1","3" "2","5","1","4" "2","6","1","3" "2","8","1","3" "3","5","2","3" "3","7","1","3" "4","6","1","3" "4","7","1","3" "4","8","1","3" "5","7","1","4" "5","8","1","4" "7","8","1","3"Reference SQL · Complete expected CSV · Empty answer CSV
Choose another SQL task · Original data exception cases · Larger workplace projects