The Lion's Share

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

Domain
SQL
Difficulty
medium
Seniority
mid
Asked in
Allstate

Problem

The merchandising finance team wants to see which product categories bring in the most money. Match every purchase to its product's category, then for each category report its total purchase revenue and how many purchases stood behind it, with the biggest earners listed first.

Every category claims its slice. Find out who really owns the table.

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
categorytotal_revenuetransaction_count
Electronics15016.820
Books12501.3919
Clothing12487.9219
Home & Kitchen1222218
Toys12090.7818

Worked solution and explanation

What this really is

This is a fact-to-dimension join with a per-group rollup, dressed up as a merchandising review. The category a purchase belongs to lives only in products, never in transactions, so the first real move is the inner join that lends each purchase its category. After that it is a GROUP BY p.category with SUM(t.total_amount) for revenue and COUNT(*) for volume. The decision that separates a reproducible answer from a flaky one is the ORDER BY: rank by the summed revenue, whose per-category values are all distinct, not by the raw purchase count, where several categories tie on exactly 18 and come back in whatever order the engine feels like.


Break down the requirements

Step 1: Borrow the category from the dimension

transactions has no category column, so each purchase must be matched to its product with a join on product_id against products. An inner join is right here: a purchase with no matching product carries no category to report on, so it drops out rather than forming a null bucket.

Step 2: Collapse to one row per category

GROUP BY p.category folds every purchase in a category into a single output row. SUM(t.total_amount) gives the category's revenue and COUNT(*) gives how many purchases produced it. COUNT(*) is a raw row count, one per transaction, not a count of distinct users or products.

Step 3: Rank by revenue, and make it total

Order by total_revenue descending so the biggest earners surface first. The reason this beats the intuitive COUNT(*) ordering is determinism: several categories tie at exactly 18 purchases, so ranking by the count leaves those rows in engine-arbitrary order, while the per-category revenue sums are all distinct and give one reproducible sequence. Note you order by the SUM, not by a raw total_amount column, which no longer exists after the grouping.


The solution

Revenue and volume by category
SELECT p.category AS category, SUM(t.total_amount) AS total_revenue, COUNT(*) AS transaction_count FROM transactions t JOIN products p ON t.product_id = p.product_id GROUP BY p.category ORDER BY total_revenue DESC;

Common follow-up questions