DB Olympics

Queries · q086 · VP Product · week 1, night 4

Late delivery and repeat purchase, September 2025 shipments

“Does a late delivery visibly reduce the chance a customer buys again, compared to an on-time delivery?”

Manager · asked the questionJonas FeldVP Product. Joined from a mobile-first retailer. Deciding the 2026 roadmap.
Analyst · planned the reportEmeka ObiCustomer-centric: follows cohorts of shoppers or merchants over time.
Engineer · wrote the SQLFelixJunior engineer who nests subqueries in FROM and WHERE (IN, EXISTS, correlated subqueries) rather than CTEs.

Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told

  1. A business event lands on the VP Product's desk

    Week 1

    We are redesigning the shopper journey for 2026. We need to understand the whole path, from the first visit to checkout, delivery, returns and the next purchase, and where shoppers drop out and why.

  2. Jonas Feld writes to Emeka Obi

    The manager, at the start of week 1

    Subject: Shopper journey end-to-end — what I need for the 2026 roadmap

    Emeka,

    We're resetting the shopper journey for 2026, and I need a clear picture of the whole path — first visit through checkout, delivery, returns, and whether people come back — so we can decide where to invest design and engineering time first. This feeds the roadmap review I'm presenting in two weeks, so the Friday memo should leave me able to say "here's where shoppers are leaking out, and here's the one or two places a fix would move the most volume."

    I know you only get a handful of report slots this week, so please prioritize in the order below — stop earlier in the list if something forces a tradeoff, rather than spreading thin across all of it.

    1. Where do we lose people between landing on the storefront and placing an order? I want to see the shape of the funnel (visit → browse/search → cart → checkout → paid order) and which step is the biggest drop-off, ideally for a few of our bigger merchants so we're not averaging away real differences.
    2. Once someone reaches checkout, what's stopping them from finishing? Is it a one-step cliff or a slow bleed across the checkout flow, and does it differ by device or by how they arrived (e.g. ad vs. direct vs. returning)?
    3. After the order is placed, how is delivery experience affecting trust? I want to understand how often deliveries run late and whether that visibly connects to returns or to people not coming back.
    4. How painful are returns, and does a return kill the relationship? I'd like a sense of how often a return leads to the shopper buying again within a reasonable window versus going quiet.
    5. Who comes back, and how soon? A simple read on repeat purchase behavior — how many first-time buyers return, and roughly how long it takes — would help me know if we're building a roadmap for acquisition or for retention.

    If you can only land three of these this week, do 1, 2 and 5 — funnel, checkout, and repeat purchase are the ones I most need going into the roadmap conversation. Delivery and returns can follow next week if we run out of nights.

    Thanks — looking forward to Friday.

    Jonas

  3. Emeka Obi plans the night

    The analyst · week 1, night 4, request 2 of 3

    Night 3 lands priority 2 solidly and complicates priority 3 in an interesting way.

    Report 1 (corrected status funnel, retail-only): confirms the clean read — pending→paid loses only 2.4% of orders, essentially instantly (median 0 hours), and paid→fulfilled is 94.9% at a sane ~23-hour median. So payment capture itself is not where retail checkout leaks; it leaks earlier, at cart→order (which Report 1 night 1 already showed at 27-61% conversion for these tenants). Refunds trickle in ~3 weeks out (517h), cancellations are near-instant (0.1h). Priority 2 is now fully answered: it's a slow bleed concentrated in the cart stage, not a cliff at payment.

    Report 2 (cart timing by tenant): the slow-bleed shape is remarkably consistent across all 6 retail tenants — <1hr bucket is ~23-32% of carts and ~100% converted (fast successful checkouts, not failures), while the 1-24hr and 1-7day buckets (roughly two-thirds of all carts, combined) convert at only 1-19%. This is a universal pattern, not one tenant's expiry setting — strong, generalizable finding for the roadmap.

    Report 3 (delivery speed vs. returns) is the interesting twist: at the merchant level, the correlation runs backwards from the "late delivery → more returns" hypothesis. The three apparel merchants (Marrowby & Vane, Studio Vrij, Cragsley Outfitters) have the highest return rates (31-34%) but some of the lowest late-delivery rates (0.3-12%), while food/beauty/pet merchants have high late-delivery rates (20-23%) but near-zero returns (0.5-4.3%). This looks like vertical (apparel fit/size returns are structurally high regardless of delivery speed) is swamping any delivery-speed effect in a merchant-level comparison. Before I tell Jonas "no connection," I want to re-test within a single vertical to remove that confound, and also test the "people not coming back" half of his question directly (repeat purchase after late delivery), which I haven't touched yet.

    This is the last night before the Friday memo. Priorities 1, 2, 5 are in good shape. I'm spending all 3 slots to get a defensible first read on the two things Jonas said could slip to next week if time ran short (delivery trust, and whether a return kills the relationship) — we have the nights, so I'll use them rather than leave those blank in the memo.

    Late delivery and repeat purchase, September 2025 shipments
    Question
    Does a late delivery visibly reduce the chance a customer buys again, compared to an on-time delivery?
    One row per
    delivery-speed bucket (on-time vs late)
    Columns
    • delivery bucket (on-time / late)
    • shipments count
    • distinct customers
    • pct of customers with another order within 90 days of delivery
    • median days to that next order
  4. Felix writes the SQL

    The engineer · 1 dry run on the empty schema, then the submission

    Dry run 1 · STATUS: COMPILES (dry run on the empty schema, no data read)
    SELECT
      delivery_bucket,
      MAX(bucket_shipment_count) AS shipments_count,
      COUNT(*) AS distinct_customers,
      ROUND(100.0 * SUM(CASE WHEN min_days_to_next IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_customers_with_next_order,
      ROUND(
        PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY min_days_to_next) FILTER (WHERE min_days_to_next IS NOT NULL)
      , 0) AS median_days_to_next_order
    FROM (
      SELECT
        tenant_id,
        user_id,
        delivery_bucket,
        MIN(days_to_next) AS min_days_to_next,
        MAX(bucket_shipment_count) AS bucket_shipment_count
      FROM (
        SELECT
          tenant_id,
          user_id,
          delivery_bucket,
          EXTRACT(EPOCH FROM (next_order_at - delivered_at)) / 86400.0 AS days_to_next,
          COUNT(*) OVER (PARTITION BY delivery_bucket) AS bucket_shipment_count
        FROM (
          SELECT
            o.tenant_id AS tenant_id,
            o.user_id AS user_id,
            CASE WHEN (s.delivered_at - s.shipped_at) <= INTERVAL '7 days' THEN 'on-time' ELSE 'late' END AS delivery_bucket,
            s.delivered_at AS delivered_at,
            (
              SELECT MIN(o2.placed_at)
              FROM orders o2
              WHERE o2.tenant_id = o.tenant_id
                AND o2.user_id = o.user_id
                AND o2.placed_at > s.delivered_at
                AND o2.placed_at <= s.delivered_at + INTERVAL '90 days'
            ) AS next_order_at
          FROM shipments s
          JOIN fulfillments f ON f.id = s.fulfillment_id
          JOIN orders o ON o.id = f.order_id
          JOIN tenants t ON t.id = o.tenant_id
          WHERE t.name IN (
              'Kibblewick', 'Studio Vrij', 'Petalcourt Beauty',
              'Marrowby & Vane', 'Cragsley Outfitters', 'Ampfeld Elektronik'
            )
            AND s.delivered_at >= DATE '2025-09-01'
            AND s.delivered_at < DATE '2025-10-01'
            AND s.shipped_at IS NOT NULL
        ) shipment_level
      ) days_level
      GROUP BY tenant_id, user_id, delivery_bucket
    ) customer_level
    GROUP BY delivery_bucket
    ORDER BY CASE WHEN delivery_bucket = 'on-time' THEN 0 ELSE 1 END, delivery_bucket

    “Query compiles and returns the expected columns. Submitting.”

    Submitted · the SQL of the last dry run, unchanged · Submitted to tonight's batch. Your work on this request is done.

    “Submitted. The report gives one row each for on-time and late shipments in September 2025 across the six retail tenants, with shipment counts, distinct customers, repeat-purchase rate within 90 days, and median days to that next order — exactly as specified, with one clarifying note on how multi-shipment customers within a bucket are collapsed to a single customer-level outcome.”

  5. The report comes back overnight

    As Emeka Obi saw it, from the small dataset the agents worked on

    delivery_bucket | shipments_count | distinct_customers | pct_customers_with_next_order | median_days_to_next_order
    ----------------+-----------------+--------------------+-------------------------------+--------------------------
    on-time         |             528 |                453 |                          58.9 |                      32.0
    late            |              12 |                 11 |                          54.5 |                      35.0
    (2 rows)
  6. What came of it

    The memo and the queries that followed; links found by title, so inferred

    It builds on:

  7. It becomes benchmark query q086

    Changed only where the benchmark needs the same result on every system

    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • numericsdocs/NUMERICS.md: EXTRACT(EPOCH FROM (b - a)) cast to NUMERIC(18,6)
    • placeholderpercentile cont
    • placeholderseconds between
    • determinismthe standard leaves the place of NULLs in a sort to the implementation: NULLS LAST ascending, NULLS FIRST descending, as in the reference results (2×)
    The benchmark text, against the submitted SQL
    SELECT  delivery_bucket,  MAX(bucket_shipment_count) AS shipments_count,  COUNT(*) AS distinct_customers,  ROUND(100.0 * SUM(CASE WHEN min_days_to_next IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_customers_with_next_order,  ROUND(    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY min_days_to_next) FILTER (WHERE min_days_to_next IS NOT NULL)  , 0) AS median_days_to_next_order  100.0 * SUM(CASE WHEN min_days_to_next IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*) AS pct_customers_with_next_order,  {{percentile_cont_filter(0.5, min_days_to_next, min_days_to_next IS NOT NULL)}} AS median_days_to_next_orderFROM (  SELECT    tenant_id,    user_id,    delivery_bucket,    MIN(days_to_next) AS min_days_to_next,    MAX(bucket_shipment_count) AS bucket_shipment_count  FROM (    SELECT      tenant_id,      user_id,      delivery_bucket,      EXTRACT(EPOCH FROM (next_order_at - delivered_at)) / 86400.0 AS days_to_next,      {{seconds_between(delivered_at, next_order_at)}} / 86400.0 AS days_to_next,      COUNT(*) OVER (PARTITION BY delivery_bucket) AS bucket_shipment_count    FROM (      SELECT        o.tenant_id AS tenant_id,        o.user_id AS user_id,        CASE WHEN (s.delivered_at - s.shipped_at) <= INTERVAL '7 days' THEN 'on-time' ELSE 'late' END AS delivery_bucket,        s.delivered_at AS delivered_at,        (          SELECT MIN(o2.placed_at)          FROM orders o2          WHERE o2.tenant_id = o.tenant_id            AND o2.user_id = o.user_id            AND o2.placed_at > s.delivered_at            AND o2.placed_at <= s.delivered_at + INTERVAL '90 days'        ) AS next_order_at      FROM shipments s      JOIN fulfillments f ON f.id = s.fulfillment_id      JOIN orders o ON o.id = f.order_id      JOIN tenants t ON t.id = o.tenant_id      WHERE t.name IN (          'Kibblewick', 'Studio Vrij', 'Petalcourt Beauty',          'Marrowby & Vane', 'Cragsley Outfitters', 'Ampfeld Elektronik'        )        AND s.delivered_at >= DATE '2025-09-01'        AND s.delivered_at < DATE '2025-10-01'        AND s.shipped_at IS NOT NULL    ) shipment_level  ) days_level  GROUP BY tenant_id, user_id, delivery_bucket) customer_levelGROUP BY delivery_bucketORDER BY CASE WHEN delivery_bucket = 'on-time' THEN 0 ELSE 1 END, delivery_bucketORDER BY CASE WHEN delivery_bucket = 'on-time' THEN 0 ELSE 1 END NULLS LAST, delivery_bucket NULLS LAST
    SELECT
      delivery_bucket,
      MAX(bucket_shipment_count) AS shipments_count,
      COUNT(*) AS distinct_customers,
      100.0 * SUM(CASE WHEN min_days_to_next IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*) AS pct_customers_with_next_order,
      PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY min_days_to_next) FILTER (WHERE min_days_to_next IS NOT NULL) AS median_days_to_next_order
    FROM (
      SELECT
        tenant_id,
        user_id,
        delivery_bucket,
        MIN(days_to_next) AS min_days_to_next,
        MAX(bucket_shipment_count) AS bucket_shipment_count
      FROM (
        SELECT
          tenant_id,
          user_id,
          delivery_bucket,
          CAST(EXTRACT(EPOCH FROM (next_order_at - delivered_at)) AS NUMERIC(18,6)) / 86400.0 AS days_to_next,
          COUNT(*) OVER (PARTITION BY delivery_bucket) AS bucket_shipment_count
        FROM (
          SELECT
            o.tenant_id AS tenant_id,
            o.user_id AS user_id,
            CASE WHEN (s.delivered_at - s.shipped_at) <= INTERVAL '7 days' THEN 'on-time' ELSE 'late' END AS delivery_bucket,
            s.delivered_at AS delivered_at,
            (
              SELECT MIN(o2.placed_at)
              FROM orders o2
              WHERE o2.tenant_id = o.tenant_id
                AND o2.user_id = o.user_id
                AND o2.placed_at > s.delivered_at
                AND o2.placed_at <= s.delivered_at + INTERVAL '90 days'
            ) AS next_order_at
          FROM shipments s
          JOIN fulfillments f ON f.id = s.fulfillment_id
          JOIN orders o ON o.id = f.order_id
          JOIN tenants t ON t.id = o.tenant_id
          WHERE t.name IN (
              'Kibblewick', 'Studio Vrij', 'Petalcourt Beauty',
              'Marrowby & Vane', 'Cragsley Outfitters', 'Ampfeld Elektronik'
            )
            AND s.delivered_at >= DATE '2025-09-01'
            AND s.delivered_at < DATE '2025-10-01'
            AND s.shipped_at IS NOT NULL
        ) shipment_level
      ) days_level
      GROUP BY tenant_id, user_id, delivery_bucket
    ) customer_level
    GROUP BY delivery_bucket
    ORDER BY CASE WHEN delivery_bucket = 'on-time' THEN 0 ELSE 1 END NULLS LAST, delivery_bucket NULLS LAST
    100 ms1 sHyper656 msDuckDB196 msGizmoSQL174 msclickhouse_brew565 msFirebolt576 msDataFusionerrorClickHouseerror
    • DataFusionThis feature is not implemented: Physical plan does not support logical expression ScalarSubquery(<subquery>)
    • ClickHouseCode: 43. DB::Exception: No operation lessOrEquals between Decimal(18, 6) and IntervalDay: while executing 'FUNCTION lessOrEquals(minus(__table6.delivered_at, __table6.shipped_at) :: 9, _CAST(7_IntervalDay, 'IntervalDay'_String) :: 5) -> lessOrEquals(minus(__table6.delivered_at, __table6.shipped_at)
  8. Everything, as recorded

    The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted

    Felix's whole session, 7 turns
    • The request, as Felix received it
      Report request from Emeka Obi (analyst for the VP Product):
      
      Title: Late delivery and repeat purchase, September 2025 shipments
      Question: Does a late delivery visibly reduce the chance a customer buys again, compared to an on-time delivery?
      Grain: one row per delivery-speed bucket (on-time vs late)
      Columns:
      - delivery bucket (on-time / late)
      - shipments count
      - distinct customers
      - pct of customers with another order within 90 days of delivery
      - median days to that next order
      Details: Restrict to the 6 retail tenants (Kibblewick, Studio Vrij, Petalcourt Beauty, Marrowby & Vane, Cragsley Outfitters, Ampfeld Elektronik) to avoid subscription-renewal noise. Base: shipments joined via fulfillments to orders (for tenant_id and the order's user_id), where shipments.delivered_at is in 2025-09-01 to 2025-09-30 (chosen so a full 90-day forward window fits before the data ends on 2025-12-31). delivery_bucket = 'on-time' if (delivered_at - shipped_at) <= 7 days, else 'late'. distinct_customers = distinct (tenant_id, user_id) behind those shipments. For each customer, check whether they placed any other order (orders.placed_at) strictly after this shipment's delivered_at and within 90 days after it; pct_of_customers = % of distinct_customers with such a subsequent order, rounded 1 decimal; median_days_to_next_order = median of (that next order's placed_at minus delivered_at) in days, among customers who did place one, rounded to a whole number. Order rows with 'on-time' first, then 'late'.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Felix calls dry_run
      SELECT
        delivery_bucket,
        MAX(bucket_shipment_count) AS shipments_count,
        COUNT(*) AS distinct_customers,
        ROUND(100.0 * SUM(CASE WHEN min_days_to_next IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_customers_with_next_order,
        ROUND(
          PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY min_days_to_next) FILTER (WHERE min_days_to_next IS NOT NULL)
        , 0) AS median_days_to_next_order
      FROM (
        SELECT
          tenant_id,
          user_id,
          delivery_bucket,
          MIN(days_to_next) AS min_days_to_next,
          MAX(bucket_shipment_count) AS bucket_shipment_count
        FROM (
          SELECT
            tenant_id,
            user_id,
            delivery_bucket,
            EXTRACT(EPOCH FROM (next_order_at - delivered_at)) / 86400.0 AS days_to_next,
            COUNT(*) OVER (PARTITION BY delivery_bucket) AS bucket_shipment_count
          FROM (
            SELECT
              o.tenant_id AS tenant_id,
              o.user_id AS user_id,
              CASE WHEN (s.delivered_at - s.shipped_at) <= INTERVAL '7 days' THEN 'on-time' ELSE 'late' END AS delivery_bucket,
              s.delivered_at AS delivered_at,
              (
                SELECT MIN(o2.placed_at)
                FROM orders o2
                WHERE o2.tenant_id = o.tenant_id
                  AND o2.user_id = o.user_id
                  AND o2.placed_at > s.delivered_at
                  AND o2.placed_at <= s.delivered_at + INTERVAL '90 days'
              ) AS next_order_at
            FROM shipments s
            JOIN fulfillments f ON f.id = s.fulfillment_id
            JOIN orders o ON o.id = f.order_id
            JOIN tenants t ON t.id = o.tenant_id
            WHERE t.name IN (
                'Kibblewick', 'Studio Vrij', 'Petalcourt Beauty',
                'Marrowby & Vane', 'Cragsley Outfitters', 'Ampfeld Elektronik'
              )
              AND s.delivered_at >= DATE '2025-09-01'
              AND s.delivered_at < DATE '2025-10-01'
              AND s.shipped_at IS NOT NULL
          ) shipment_level
        ) days_level
        GROUP BY tenant_id, user_id, delivery_bucket
      ) customer_level
      GROUP BY delivery_bucket
      ORDER BY CASE WHEN delivery_bucket = 'on-time' THEN 0 ELSE 1 END, delivery_bucket
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: delivery_bucket, shipments_count, distinct_customers, pct_customers_with_next_order, median_days_to_next_order
    • FelixQuery compiles and returns the expected columns. Submitting.
    • Felix calls submit
      SELECT
        delivery_bucket,
        MAX(bucket_shipment_count) AS shipments_count,
        COUNT(*) AS distinct_customers,
        ROUND(100.0 * SUM(CASE WHEN min_days_to_next IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_customers_with_next_order,
        ROUND(
          PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY min_days_to_next) FILTER (WHERE min_days_to_next IS NOT NULL)
        , 0) AS median_days_to_next_order
      FROM (
        SELECT
          tenant_id,
          user_id,
          delivery_bucket,
          MIN(days_to_next) AS min_days_to_next,
          MAX(bucket_shipment_count) AS bucket_shipment_count
        FROM (
          SELECT
            tenant_id,
            user_id,
            delivery_bucket,
            EXTRACT(EPOCH FROM (next_order_at - delivered_at)) / 86400.0 AS days_to_next,
            COUNT(*) OVER (PARTITION BY delivery_bucket) AS bucket_shipment_count
          FROM (
            SELECT
              o.tenant_id AS tenant_id,
              o.user_id AS user_id,
              CASE WHEN (s.delivered_at - s.shipped_at) <= INTERVAL '7 days' THEN 'on-time' ELSE 'late' END AS delivery_bucket,
              s.delivered_at AS delivered_at,
              (
                SELECT MIN(o2.placed_at)
                FROM orders o2
                WHERE o2.tenant_id = o.tenant_id
                  AND o2.user_id = o.user_id
                  AND o2.placed_at > s.delivered_at
                  AND o2.placed_at <= s.delivered_at + INTERVAL '90 days'
              ) AS next_order_at
            FROM shipments s
            JOIN fulfillments f ON f.id = s.fulfillment_id
            JOIN orders o ON o.id = f.order_id
            JOIN tenants t ON t.id = o.tenant_id
            WHERE t.name IN (
                'Kibblewick', 'Studio Vrij', 'Petalcourt Beauty',
                'Marrowby & Vane', 'Cragsley Outfitters', 'Ampfeld Elektronik'
              )
              AND s.delivered_at >= DATE '2025-09-01'
              AND s.delivered_at < DATE '2025-10-01'
              AND s.shipped_at IS NOT NULL
          ) shipment_level
        ) days_level
        GROUP BY tenant_id, user_id, delivery_bucket
      ) customer_level
      GROUP BY delivery_bucket
      ORDER BY CASE WHEN delivery_bucket = 'on-time' THEN 0 ELSE 1 END, delivery_bucket
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • FelixSubmitted. The report gives one row each for on-time and late shipments in September 2025 across the six retail tenants, with shipment counts, distinct customers, repeat-purchase rate within 90 days, and median days to that next order — exactly as specified, with one clarifying note on how multi-shipment customers within a bucket are collapsed to a single customer-level outcome.