Skip to main content
Back to Academy📊 3. Analytical SQL & Performance Optimization
5 min read
Try Hands-on Lab
VISUAL LESSON3. Analytical SQL & Performance Optimization

Inventory Aging, Sell-Through & Marketplace Funnel Analytics

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.

Marketplace Sell-Through Rate SQLInventory Aging Buckets SQLUnsold Inventory MetricsLEFT JOIN Conversion FunnelNULL-Safe Marketplace KPICohort Conversion Analytics

Explain Like I'm New: The Bakery Display Case at Closing Time

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!

Key Beginner Takeaway: Measuring Marketplace Conversion & Inventory Health requires starting from Supply (`fact_listing`) on the LEFT, LEFT JOINing aggregated Sales, treating NULL sales as `0`, and bucketing remaining stock by Age (`CASE WHEN`)!

Core Marketplace & Retail Metric Formulas

Metric NameFormulaWhat It Tells Business Leaders
Sell-Through Rate (%)(Units Sold / NULLIF(Units Listed, 0)) * 100How efficiently listed inventory turns into cash. Low % = overpriced or weak demand.
Unsold InventoryUnits 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)) * 100Whether sellers had to slash prices steeply to clear inventory.

Step-by-Step Worked SQL: Zero-Bug Marketplace Conversion & Aging

Suppose `fact_listing` has 3 listings, and `fact_sales` only recorded checkouts on 2 of them:

`fact_listing`

listing_idcategorylisted_datenumticketsprice_per_ticket
10Concerts2026-04-014100.00
20Concerts2026-04-202120.00
30Theater2026-03-15690.00

`fact_sales`

sale_idlisting_idqty_soldtotal_paid
901103300.00
902202240.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:

categorytotal_listedtotal_soldtotal_unsoldsell_through_pctstale_30d_plus_qty
Concerts65183.331 (Listing 10 is 30d old!)
Theater6060.006 (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!


Common Beginner Mistakes (And How to Fix Them)

  1. Using `INNER JOIN` on `fact_sales`: Drops all 0% sell-through listings (Theater above completely vanishes!), inflating total marketplace sell-through falsely!
  2. Computing `AVG(sell_through_per_listing)` instead of weighted `SUM(sold) / SUM(listed)`: Averaging (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!
  3. Forgetting `COALESCE` before subtraction (`listed - NULL = NULL`): Remember 6 - NULL evaluates to NULL, hiding all unsold Theater inventory! Always wrap nullable joins with COALESCE(s.sold_qty, 0).

Quick Cheat Sheet

  1. Anchor inventory & conversion funnels on `fact_listing` (`LEFT JOIN`) so zero-sale supply stays visible.
  2. Pre-aggregate fact_sales by listing_id before joining to prevent supply duplication.
  3. Compute Weighted Sell-Through via SUM(sold) * 100.0 / NULLIF(SUM(listed), 0).
  4. Bucket unsold items with CASE WHEN age tiers (0-7d, 8-29d, 30+d) to surface deadstock early.

Comprehension & Architecture Check (3 Questions)

Test your understanding of the concepts covered in this lesson.

1. Why must listings and sales be aggregated in CTEs prior to computing event sell-through?
2. Why should beginners check a table's grain before writing a JOIN?
3. Which Medallion layer is meant to store clean, deduplicated, standardized records?
Practice What You Learned

TICKIT-06: Inventory Sell-Through & Event Funnel Conversion

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.

Solve Challenge