Category Revenue

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
L3

Problem

The finance team is assembling a category performance slide for the quarterly business review. For each product category they want the total revenue, the number of transactions behind it, and the average transaction size, leaving off any category whose total revenue comes in under $12,100. List the categories from highest revenue to lowest.

Which categories pull their weight?

Tables

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

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

Expected output

First 4 of 4 rows
categorytotal_revenuetransaction_countavg_transaction_size
Electronics15016.820750.8399999999999
Books12501.3919657.967894736842
Clothing12487.9219657.2589473684211
Home & Kitchen1222218679

Worked solution and explanation

What this problem is really about

Underneath the slide costume this is a threshold on a grouped total. Anyone can total revenue per category; the tell is where the $12,100 cutoff lands. It compares each category's SUM(total_amount), a number that does not exist until the rows are grouped, so it cannot live in WHERE. Reference the aggregate there and the query errors. Test total_amount there instead and you are filtering single sales, none of which comes anywhere near $12,100, so the slide comes back empty. Forget the cutoff entirely and every category, the two-product Collectibles line included, lands in front of the executives.


Break down the requirements

Step 1: Join products to transactions

Join on product_id to attach category to each transaction. The category lives only on products, so every sale has to be fanned out to its category before anything can be totaled. Sales whose product_id is empty have no category to land in, and the inner join drops them.

Step 2: Aggregate three metrics

SUM(t.total_amount) for revenue, COUNT(*) for transaction count, and AVG(t.total_amount) for average transaction size, all grouped by p.category in a single pass.

Step 3: Filter on the total, then sort

HAVING SUM(t.total_amount) >= 12100 drops the categories that fall under the slide cutoff. This has to be HAVING, not WHERE, because the value being tested is the grouped total. Then ORDER BY total_revenue DESC puts the survivors highest first.


The solution

Multi-metric aggregation with a HAVING threshold
SELECT
    p.category,
    SUM(t.total_amount) AS total_revenue,
    COUNT(*) AS transaction_count,
    AVG(t.total_amount) AS avg_transaction_size
FROM products p
JOIN transactions t ON p.product_id = t.product_id
GROUP BY p.category
HAVING SUM(t.total_amount) >= 12100
ORDER BY total_revenue DESC

Common follow-up questions