Build reliable marketplace funnel KPIs (Sell-Through Rate, Inventory Aging Buckets, Unsold Deadstock) cleanly in SQL without dropping zero-sale listings or causing divide-by-zero errors.
Imagine you manage a bakery:
- At 7:00 AM you bake 100 croissants (Listed Supply).
- By 5:00 PM you sold 78 croissants (Sold Demand), leaving 22 croissants sitting unsold on the tray (Unsold Inventory).
Your Sell-Through Rate is 78 / 100 = 78%.
Now imagine one tray of Blueberry Scones (`20 baked`) sold `0` today!
If your SQL uses an INNER JOIN between baked_trays and checkout_receipts, the Blueberry Scones disappear completely, telling your boss *"100% of items sold cleanly!"* — while 20 stale scones sit untouched in the display case!
LEFT JOINing aggregated Sales, treating NULL sales as `0`, and bucketing remaining stock by Age (`CASE WHEN`)!| Metric Name | Formula | What It Tells Business Leaders |
|---|---|---|
| Sell-Through Rate (%) | (Units Sold / NULLIF(Units Listed, 0)) * 100 | How efficiently listed inventory turns into cash. Low % = overpriced or weak demand. |
| Unsold Inventory | Units Listed - COALESCE(Units Sold, 0) | Remaining open supply still needing markdowns or promotion. |
| Inventory Aging (Days Unsold) | DATEDIFF(CURRENT_DATE, listed_date) | How long stock has sat stagnant (0-7d, 8-30d, 31+d deadstock). |
| Listing Realization Rate (%) | (Actual Gross Sales / NULLIF(Initial Asking Value, 0)) * 100 | Whether sellers had to slash prices steeply to clear inventory. |
Suppose `fact_listing` has 3 listings, and `fact_sales` only recorded checkouts on 2 of them:
| listing_id | category | listed_date | numtickets | price_per_ticket |
|---|---|---|---|---|
| 10 | Concerts | 2026-04-01 | 4 | 100.00 |
| 20 | Concerts | 2026-04-20 | 2 | 120.00 |
| 30 | Theater | 2026-03-15 | 6 | 90.00 |
| sale_id | listing_id | qty_sold | total_paid |
|---|---|---|---|
| 901 | 10 | 3 | 300.00 |
| 902 | 20 | 2 | 240.00 |
Notice Listing `30` (Theater, `6` tickets) has ZERO rows in `fact_sales`! Let's write a bulletproof conversion + aging report:
WITH sales_per_listing AS (
-- Step 1: Pre-aggregate sales to 1 row per listing_id to avoid fan-out!
SELECT
listing_id,
SUM(qty_sold) AS sold_qty,
SUM(total_paid) AS realized_gmv
FROM fact_sales
GROUP BY listing_id
),
listing_funnel AS (
-- Step 2: Anchor on Supply (fact_listing) via LEFT JOIN so zero-sale listings survive!
SELECT
l.listing_id,
l.category,
l.listed_date,
l.numtickets AS listed_qty,
COALESCE(s.sold_qty, 0) AS sold_qty,
l.numtickets - COALESCE(s.sold_qty, 0) AS unsold_qty,
DATEDIFF(DATE '2026-05-01', l.listed_date) AS days_listed,
COALESCE(s.realized_gmv, 0.00) AS realized_gmv
FROM fact_listing l
LEFT JOIN sales_per_listing s
ON l.listing_id = s.listing_id
)
SELECT
category,
SUM(listed_qty) AS total_listed,
SUM(sold_qty) AS total_sold,
SUM(unsold_qty) AS total_unsold,
ROUND(SUM(sold_qty) * 100.0 / NULLIF(SUM(listed_qty), 0), 2) AS sell_through_pct,
SUM(CASE WHEN unsold_qty > 0 AND days_listed >= 30 THEN unsold_qty ELSE 0 END) AS stale_30d_plus_qty
FROM listing_funnel
GROUP BY category;Output:
| category | total_listed | total_sold | total_unsold | sell_through_pct | stale_30d_plus_qty |
|---|---|---|---|---|---|
| Concerts | 6 | 5 | 1 | 83.33 | 1 (Listing 10 is 30d old!) |
| Theater | 6 | 0 | 6 | 0.00 | 6 (Listing 30 is 47d old!) |
Look how actionable that output is: Theater immediately pops up with 0.00% sell-through and 6 aged 30d+ tickets needing price cuts!
0% sell-through listings (Theater above completely vanishes!), inflating total marketplace sell-through falsely!(1/100 = 1%) and (1/1 = 100%) gives 50.5%, when really only 2 / 101 = 1.98% of inventory sold! Always aggregate numerator & denominator sums first!6 - NULL evaluates to NULL, hiding all unsold Theater inventory! Always wrap nullable joins with COALESCE(s.sold_qty, 0).fact_sales by listing_id before joining to prevent supply duplication.SUM(sold) * 100.0 / NULLIF(SUM(listed), 0).CASE WHEN age tiers (0-7d, 8-29d, 30+d) to surface deadstock early.Test your understanding of the concepts covered in this lesson.
Event organizers need to track how quickly tickets sell out after being listed. We calculate the sell-through percentage (tickets sold divided by tickets listed) across each event category.