SQL interview question: category pairs bought together

MediumPairs and self-joins

Problem

Shared data rules

For each pair of categories, count the September 2026 orders that contain both. Also find their share of all September orders.

Formula

Example

Input: orders
order_idcreated_atstatus
1012026-09-02 13:00delivered
1022026-09-05 18:20delivered
1032026-09-08 11:00delivered
Input: order_items
order_idcategoryRow note
101Produce
101Produce← second item, same category
101Meat & seafood
102Meat & seafood
102Produce
103Pantry
Output
category_acategory_bordersshare
Meat & seafoodProduce20.6667

The pair “Meat & seafood” — “Produce” is in 2 orders out of 3: a share of 0.6667. Order 103 with a single category still counts in the denominator.

Notes

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions