DB Olympics

Queries · q056 · Head of Growth Marketing · week 1, night 3

Email/campaign engagement baseline by channel

“How much campaign messaging goes out, and is anyone opening or clicking — is this a strength or a weak spot across the platform?”

Manager · asked the questionChloé MarchettiHead of Growth Marketing. Joined from a direct-to-consumer brand. Wants to sell merchants more of Tidewick's marketing tools.
Analyst · planned the reportSofie NilsenStoryteller: looks for the two or three findings that matter and digs until each is explained.
Engineer · wrote the SQLGraceWrites tidy, layered CTEs with descriptive names, one step per CTE, then a final select.

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

  1. Chloé Marchetti writes to Sofie Nilsen

    The manager, at the start of week 1

    Subject: What I need to get oriented this week

    Hi Sofie,

    Thrilled to be here — here's where my head's at. I know you've only got a handful of report slots a night, so I've ordered these by what I need most. Please push back if any of these are too broad or should split into more than one report.

    1. How are merchants actually acquiring shoppers, and through which channels? I want to understand where traffic and new customers are coming from across our merchant base — which channels show up most, and whether that's shifted recently. This is really just orientation for me: I don't yet know what "normal" looks like here.
    2. Are the coupons and promotions merchants run actually paying off? I'd like a sense of how much discounting is happening, how often codes get used, and whether promoted orders look healthy (not just cheaper). I'm trying to figure out if we should be telling merchants to promote more carefully — this is relevant for your product-pitch angle too.
    3. What does our email/campaign activity look like, and is anyone opening or clicking? I want a baseline read on send volume and engagement so I know whether campaigns are a strength or a weak spot for merchants on the platform.
    4. Is anyone actually using loyalty and referrals, and does it seem to matter? I keep hearing these are "nice to have" features. I want to know whether merchants who use them have any different shopper behavior, so I know whether to invest in pushing adoption.
    5. (If there's room this week) Do we run experiments/A-B tests at all, and does anyone act on the results? Lower priority — just curious whether this is a muscle we have or one I need to build from scratch.

    What I need this for: I'm meeting the leadership team Friday and want a grounded, numbers-based view of growth marketing's current state before I propose anything. No need for polish — I just want to know what's really happening so I don't propose things that are already working, or miss things that are quietly broken.

    Thanks for taking this on — let me know if you want me to cut this list down.

    Chloé

  2. Sofie Nilsen plans the night

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

    The Jan/Jul discount-rate report flagged that two B2B wholesalers (Castell Fasteners, OfficeHub Nordic) run discounts on 65-95% of orders even outside the spike months — that's almost certainly negotiated/contract pricing, not promotional codes. That's a real caveat to last night's 'discounted orders look healthier' finding (higher AOV, more items, fewer returns), since B2B bulk orders would naturally look that way regardless of discounting. Before I tell Chloé 'discounting is healthy,' I need to check it holds in consumer verticals, not just because B2B is mixed in. That's tonight's top priority. With that resolved, I'm moving on to orientation baselines for Q3 (email/campaign engagement) and Q4 (loyalty), since two nights remain and those questions haven't been touched yet. Direct-traffic concentration (Report 1 last night) came back as a straightforward big-merchant-volume story, not an anomaly, so I'm not spending a slot chasing it further.

    Email/campaign engagement baseline by channel
    Question
    How much campaign messaging goes out, and is anyone opening or clicking — is this a strength or a weak spot across the platform?
    One row per
    campaign channel (campaigns.channel)
    Columns
    • channel - campaigns.channel
    • messages_sent - count of campaign_messages with sent_at not null, created_at in 2025
    • delivered_rate_pct - share of sent messages with delivered_at not null, *100, rounded to 1 decimal
    • open_rate_pct - share of delivered messages with opened_at not null, *100, rounded to 1 decimal
    • click_rate_pct - share of delivered messages with clicked_at not null, *100, rounded to 1 decimal
    • bounce_rate_pct - share of sent messages with bounced_at not null, *100, rounded to 1 decimal
    • unsubscribe_rate_pct - share of sent messages with unsubscribed_at not null, *100, rounded to 1 decimal
  3. Grace 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)
    WITH messages AS (
      SELECT
        cm.sent_at,
        cm.delivered_at,
        cm.opened_at,
        cm.clicked_at,
        cm.bounced_at,
        cm.unsubscribed_at,
        c.channel
      FROM campaign_messages cm
      JOIN campaigns c ON c.id = cm.campaign_id
      WHERE cm.created_at >= DATE '2025-01-01'
        AND cm.created_at < DATE '2026-01-01'
    ),
    agg AS (
      SELECT
        channel,
        COUNT(*) FILTER (WHERE sent_at IS NOT NULL) AS messages_sent,
        COUNT(*) FILTER (WHERE delivered_at IS NOT NULL) AS delivered_count,
        COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND opened_at IS NOT NULL) AS opened_count,
        COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND clicked_at IS NOT NULL) AS clicked_count,
        COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND bounced_at IS NOT NULL) AS bounced_count,
        COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND unsubscribed_at IS NOT NULL) AS unsubscribed_count
      FROM messages
      GROUP BY channel
    )
    SELECT
      channel,
      messages_sent,
      ROUND(100.0 * delivered_count / NULLIF(messages_sent, 0), 1) AS delivered_rate_pct,
      ROUND(100.0 * opened_count / NULLIF(delivered_count, 0), 1) AS open_rate_pct,
      ROUND(100.0 * clicked_count / NULLIF(delivered_count, 0), 1) AS click_rate_pct,
      ROUND(100.0 * bounced_count / NULLIF(messages_sent, 0), 1) AS bounce_rate_pct,
      ROUND(100.0 * unsubscribed_count / NULLIF(messages_sent, 0), 1) AS unsubscribe_rate_pct
    FROM agg
    ORDER BY messages_sent DESC, channel ASC

    “Query compiles and matches the requested grain and 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 per campaign channel with messages sent in 2025 and the delivered/open/click/bounce/unsubscribe rates as specified, ordered by volume so the Head of Growth Marketing can quickly see which channels carry the most engagement risk or strength.”

  4. The report comes back overnight

    As Sofie Nilsen saw it, from the small dataset the agents worked on

    channel | messages_sent | delivered_rate_pct | open_rate_pct | click_rate_pct | bounce_rate_pct | unsubscribe_rate_pct
    --------+---------------+--------------------+---------------+----------------+-----------------+---------------------
    email   |        106141 |               99.5 |          29.9 |            2.4 |             0.3 |                  0.2
    (1 rows)
  5. It becomes benchmark query q056

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

    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • made explicita selected t.col gets AS col, its standard name made explicit (7×)
    • 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
    WITH messages AS (  SELECT    cm.sent_at,    cm.delivered_at,    cm.opened_at,    cm.clicked_at,    cm.bounced_at,    cm.unsubscribed_at,    c.channel    cm.sent_at AS sent_at,    cm.delivered_at AS delivered_at,    cm.opened_at AS opened_at,    cm.clicked_at AS clicked_at,    cm.bounced_at AS bounced_at,    cm.unsubscribed_at AS unsubscribed_at,    c.channel AS channel  FROM campaign_messages cm  JOIN campaigns c ON c.id = cm.campaign_id  WHERE cm.created_at >= DATE '2025-01-01'    AND cm.created_at < DATE '2026-01-01'),agg AS (  SELECT    channel,    COUNT(*) FILTER (WHERE sent_at IS NOT NULL) AS messages_sent,    COUNT(*) FILTER (WHERE delivered_at IS NOT NULL) AS delivered_count,    COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND opened_at IS NOT NULL) AS opened_count,    COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND clicked_at IS NOT NULL) AS clicked_count,    COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND bounced_at IS NOT NULL) AS bounced_count,    COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND unsubscribed_at IS NOT NULL) AS unsubscribed_count  FROM messages  GROUP BY channel)SELECT  channel,  messages_sent,  ROUND(100.0 * delivered_count / NULLIF(messages_sent, 0), 1) AS delivered_rate_pct,  ROUND(100.0 * opened_count / NULLIF(delivered_count, 0), 1) AS open_rate_pct,  ROUND(100.0 * clicked_count / NULLIF(delivered_count, 0), 1) AS click_rate_pct,  ROUND(100.0 * bounced_count / NULLIF(messages_sent, 0), 1) AS bounce_rate_pct,  ROUND(100.0 * unsubscribed_count / NULLIF(messages_sent, 0), 1) AS unsubscribe_rate_pct  100.0 * delivered_count / NULLIF(messages_sent, 0) AS delivered_rate_pct,  100.0 * opened_count / NULLIF(delivered_count, 0) AS open_rate_pct,  100.0 * clicked_count / NULLIF(delivered_count, 0) AS click_rate_pct,  100.0 * bounced_count / NULLIF(messages_sent, 0) AS bounce_rate_pct,  100.0 * unsubscribed_count / NULLIF(messages_sent, 0) AS unsubscribe_rate_pctFROM aggORDER BY messages_sent DESC, channel ASCORDER BY messages_sent DESC NULLS FIRST, channel ASC NULLS LAST
    WITH messages AS (
      SELECT
        cm.sent_at AS sent_at,
        cm.delivered_at AS delivered_at,
        cm.opened_at AS opened_at,
        cm.clicked_at AS clicked_at,
        cm.bounced_at AS bounced_at,
        cm.unsubscribed_at AS unsubscribed_at,
        c.channel AS channel
      FROM campaign_messages cm
      JOIN campaigns c ON c.id = cm.campaign_id
      WHERE cm.created_at >= DATE '2025-01-01'
        AND cm.created_at < DATE '2026-01-01'
    ),
    agg AS (
      SELECT
        channel,
        COUNT(*) FILTER (WHERE sent_at IS NOT NULL) AS messages_sent,
        COUNT(*) FILTER (WHERE delivered_at IS NOT NULL) AS delivered_count,
        COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND opened_at IS NOT NULL) AS opened_count,
        COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND clicked_at IS NOT NULL) AS clicked_count,
        COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND bounced_at IS NOT NULL) AS bounced_count,
        COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND unsubscribed_at IS NOT NULL) AS unsubscribed_count
      FROM messages
      GROUP BY channel
    )
    SELECT
      channel,
      messages_sent,
      100.0 * delivered_count / NULLIF(messages_sent, 0) AS delivered_rate_pct,
      100.0 * opened_count / NULLIF(delivered_count, 0) AS open_rate_pct,
      100.0 * clicked_count / NULLIF(delivered_count, 0) AS click_rate_pct,
      100.0 * bounced_count / NULLIF(messages_sent, 0) AS bounce_rate_pct,
      100.0 * unsubscribed_count / NULLIF(messages_sent, 0) AS unsubscribe_rate_pct
    FROM agg
    ORDER BY messages_sent DESC NULLS FIRST, channel ASC NULLS LAST
    10 ms100 ms1 s10 sHyper216 msDuckDB78.8 msGizmoSQL137 msclickhouse_brew789 msFirebolt1.05 sDataFusion176 msClickHouse211 ms
  6. 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

    Grace's whole session, 7 turns
    • The request, as Grace received it
      Report request from Sofie Nilsen (analyst for the Head of Growth Marketing):
      
      Title: Email/campaign engagement baseline by channel
      Question: How much campaign messaging goes out, and is anyone opening or clicking — is this a strength or a weak spot across the platform?
      Grain: one row per campaign channel (campaigns.channel)
      Columns:
      - channel - campaigns.channel
      - messages_sent - count of campaign_messages with sent_at not null, created_at in 2025
      - delivered_rate_pct - share of sent messages with delivered_at not null, *100, rounded to 1 decimal
      - open_rate_pct - share of delivered messages with opened_at not null, *100, rounded to 1 decimal
      - click_rate_pct - share of delivered messages with clicked_at not null, *100, rounded to 1 decimal
      - bounce_rate_pct - share of sent messages with bounced_at not null, *100, rounded to 1 decimal
      - unsubscribe_rate_pct - share of sent messages with unsubscribed_at not null, *100, rounded to 1 decimal
      Details: Source: campaign_messages joined to campaigns on campaign_id for channel. Filter campaign_messages.created_at between 2025-01-01 and 2026-01-01 (platform-wide, no tenant filter). messages_sent = sent_at is not null; all rate denominators as specified (delivered/open/click relative to delivered count where noted, bounce/unsubscribe relative to sent count). Order by messages_sent descending, tie-break channel ascending. Expect a small number of distinct channel values, well under the row limit.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Grace calls dry_run
      WITH messages AS (
        SELECT
          cm.sent_at,
          cm.delivered_at,
          cm.opened_at,
          cm.clicked_at,
          cm.bounced_at,
          cm.unsubscribed_at,
          c.channel
        FROM campaign_messages cm
        JOIN campaigns c ON c.id = cm.campaign_id
        WHERE cm.created_at >= DATE '2025-01-01'
          AND cm.created_at < DATE '2026-01-01'
      ),
      agg AS (
        SELECT
          channel,
          COUNT(*) FILTER (WHERE sent_at IS NOT NULL) AS messages_sent,
          COUNT(*) FILTER (WHERE delivered_at IS NOT NULL) AS delivered_count,
          COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND opened_at IS NOT NULL) AS opened_count,
          COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND clicked_at IS NOT NULL) AS clicked_count,
          COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND bounced_at IS NOT NULL) AS bounced_count,
          COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND unsubscribed_at IS NOT NULL) AS unsubscribed_count
        FROM messages
        GROUP BY channel
      )
      SELECT
        channel,
        messages_sent,
        ROUND(100.0 * delivered_count / NULLIF(messages_sent, 0), 1) AS delivered_rate_pct,
        ROUND(100.0 * opened_count / NULLIF(delivered_count, 0), 1) AS open_rate_pct,
        ROUND(100.0 * clicked_count / NULLIF(delivered_count, 0), 1) AS click_rate_pct,
        ROUND(100.0 * bounced_count / NULLIF(messages_sent, 0), 1) AS bounce_rate_pct,
        ROUND(100.0 * unsubscribed_count / NULLIF(messages_sent, 0), 1) AS unsubscribe_rate_pct
      FROM agg
      ORDER BY messages_sent DESC, channel ASC
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: channel, messages_sent, delivered_rate_pct, open_rate_pct, click_rate_pct, bounce_rate_pct, unsubscribe_rate_pct
    • GraceQuery compiles and matches the requested grain and columns. Submitting.
    • Grace calls submit
      WITH messages AS (
        SELECT
          cm.sent_at,
          cm.delivered_at,
          cm.opened_at,
          cm.clicked_at,
          cm.bounced_at,
          cm.unsubscribed_at,
          c.channel
        FROM campaign_messages cm
        JOIN campaigns c ON c.id = cm.campaign_id
        WHERE cm.created_at >= DATE '2025-01-01'
          AND cm.created_at < DATE '2026-01-01'
      ),
      agg AS (
        SELECT
          channel,
          COUNT(*) FILTER (WHERE sent_at IS NOT NULL) AS messages_sent,
          COUNT(*) FILTER (WHERE delivered_at IS NOT NULL) AS delivered_count,
          COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND opened_at IS NOT NULL) AS opened_count,
          COUNT(*) FILTER (WHERE delivered_at IS NOT NULL AND clicked_at IS NOT NULL) AS clicked_count,
          COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND bounced_at IS NOT NULL) AS bounced_count,
          COUNT(*) FILTER (WHERE sent_at IS NOT NULL AND unsubscribed_at IS NOT NULL) AS unsubscribed_count
        FROM messages
        GROUP BY channel
      )
      SELECT
        channel,
        messages_sent,
        ROUND(100.0 * delivered_count / NULLIF(messages_sent, 0), 1) AS delivered_rate_pct,
        ROUND(100.0 * opened_count / NULLIF(delivered_count, 0), 1) AS open_rate_pct,
        ROUND(100.0 * clicked_count / NULLIF(delivered_count, 0), 1) AS click_rate_pct,
        ROUND(100.0 * bounced_count / NULLIF(messages_sent, 0), 1) AS bounce_rate_pct,
        ROUND(100.0 * unsubscribed_count / NULLIF(messages_sent, 0), 1) AS unsubscribe_rate_pct
      FROM agg
      ORDER BY messages_sent DESC, channel ASC
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • GraceSubmitted. The report gives one row per campaign channel with messages sent in 2025 and the delivered/open/click/bounce/unsubscribe rates as specified, ordered by volume so the Head of Growth Marketing can quickly see which channels carry the most engagement risk or strength.