Median Transaction by Category

A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
hard
Seniority
L4

Problem

Compute the median transaction amount for each product category. Show each category alongside its median, walking the categories in alphabetical order.

The middle transaction in each category.

Tables

transactions

Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date TEXT

transactions: 10 of 200 rows
transaction_iduser_idproduct_idquantitytotal_amounttransaction_date
10671971001223.462026-02-02
11342941050336.932026-03-03
12013911099450.42026-04-04
12684881148563.872026-05-05
13355851197002026-06-06
14026821246290.812026-07-07
146977912953104.282026-08-08
153687613444117.752026-09-09
160397313935-452026-01-10
1670107014421144.692026-02-11

products

Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock INTEGER

products: 10 of 200 rows
product_idproduct_namecategorypriceratingin_stock
1001Basic Device 1XBooks17.522.71
1050Deluxe Bundle 2XClothing25.054.41
1099Pro Unit 3XHome & Kitchen32.582.11
1148Ultra Tool 4XCollectibles40.113.81
1197Essential Set 5XToys47.641.51
1246Classic System 6XBeauty03.21
1295Smart Gadget 7XAutomotive62.74.90
1344Eco Kit 8XGarden70.232.61
1393Turbo Pack 9XMusic77.764.30
1442Premium Widget 10XElectronics85.2921

Expected output

First 5 of 11 rows
categorymedian_amount
Automotive643.08
Beauty629.61
Books696.96
Clothing710.43
Collectibles131.22

Worked solution and explanation

What this problem really is

This is an exact per-group median dressed up as a dashboard metric. There is no one-call median here, so you have to find the middle of each category by hand: number every categorized sale from cheapest to dearest inside its category, count the category, and keep the middle position. Most candidates get that far. The even-sized category is what separates them. There is no single middle row there, and a candidate who picks one of the two central sales reports a median off by half the gap between them. The second, quieter trap is the join: sales with no product_id belong to no category and must drop out before anything is counted.

Build it in three moves

Step 1: Attach the category before numbering anything

The category lives on products, so inner-join transactions to products on product_id first. The window can only partition by p.category once every sale carries its category, and the same inner join silently drops the sales whose product_id is empty.

Step 2: Number each sale and size its category

ROW_NUMBER() OVER (PARTITION BY p.category ORDER BY t.total_amount) gives each sale its position from the cheapest up, and COUNT(*) OVER (PARTITION BY p.category) stamps the category size on every row without collapsing them. Both windows share one partition, so each category is sorted once.

Step 3: Keep the middle one or two rows, then average

With integer division, (cnt + 1) / 2 and (cnt + 2) / 2 land on the same position when cnt is odd and on the two central positions when it is even. Filter on that pair, then take AVG(total_amount) per category: one row averages to itself, and two rows average to the true median.

Join first, then the middle-pair median
WITH ranked AS (
  SELECT p.category, t.total_amount,
         ROW_NUMBER() OVER (PARTITION BY p.category ORDER BY t.total_amount) AS rn,
         COUNT(*) OVER (PARTITION BY p.category) AS cnt
  FROM transactions t INNER JOIN products p ON t.product_id = p.product_id
)
SELECT category, AVG(total_amount) AS median_amount
FROM ranked
WHERE rn IN ((cnt + 1) / 2, (cnt + 2) / 2)
GROUP BY category
ORDER BY category
Picks one middle row
WHERE rn = (cnt + 1) / 2 is exact on odd categories. On an even one it returns the lower central sale, so median_amount sits below the real median by half the gap to its neighbour.
Averages the middle pair
WHERE rn IN ((cnt + 1) / 2, (cnt + 2) / 2) keeps one row or two, and the outer AVG turns either case into the right answer with no branching on parity.

Common follow-up questions