KatInterview

Fix a Promotion Join That Silently Drops Sales Rows

SQL & DataModel answer
MediumModel answerSQL & Datajoinsdatetimewindow-functions

Last updated

Diagnose and repair a multi-table SQL query that loses rows due to a NULL-breaking join, UTC date bucketing, and promotion fan-out inflation.

A retail analytics team tracks seasonal promotions across a chain of garden supply stores. Their reporting query is supposed to return daily sales figures for Mulch products during the Garden Season Promo, broken out by date, store, and product. But when they run it, the result set is much smaller than expected — whole stores seem to vanish from the output.

Schema

orders

ColumnType
order_idBIGINT
customer_idBIGINT
store_idINT
order_ts_utcTIMESTAMP
order_statusVARCHAR

order_items

ColumnType
order_idBIGINT
line_idINT
product_idINT
quantityINT
unit_priceNUMERIC(10,2)

products

ColumnType
product_idINT
product_nameVARCHAR
categoryVARCHAR

promotions

ColumnType
promo_idINT
promo_nameVARCHAR
product_idINT
store_idINT NULL
start_dateDATE
end_dateDATE
is_nationalBOOLEAN

stores

ColumnType
store_idINT
regionVARCHAR
timezoneVARCHAR

Business rules

  • Analyze orders placed between 2025-03-01 and 2025-05-31.
  • Only include orders where order_status = 'COMPLETED'.
  • Only include products where category = 'Mulch'.
  • All reporting dates must reflect the America/New_York business timezone, regardless of each store's local timezone.
  • A promotion is either:
    • Store-specific — promotions.store_id is set and applies only to that store.
    • National — promotions.store_id is NULL and applies to every store.

Expected output

One row per (order_date_et, store_id, product_id) with:

  • units_sold — total quantity
  • gross_revenue — total quantity × unit_price

The buggy query

SELECT
    DATE(o.order_ts_utc)    AS order_date_et,
    s.store_id,
    p.product_id,
    SUM(oi.quantity)                   AS units_sold,
    SUM(oi.quantity * oi.unit_price)   AS gross_revenue
FROM orders o
JOIN order_items oi  ON o.order_id    = oi.order_id
JOIN products   p   ON oi.product_id  = p.product_id
JOIN promotions pr  ON p.product_id   = pr.product_id
JOIN stores     s   ON pr.store_id    = s.store_id
WHERE o.order_status = 'COMPLETED'
  AND p.category     = 'Mulch'
  AND pr.promo_name  = 'Garden Season Promo'
  AND DATE(o.order_ts_utc) BETWEEN pr.start_date AND pr.end_date
  AND o.store_id = s.store_id
GROUP BY 1, 2, 3;

Your task

  1. Explain why this query returns far fewer rows than expected.
  2. Identify every logical bug — focus on join conditions and date handling.
  3. Rewrite the query so it correctly captures all qualifying mulch orders, including both store-specific and national promotions.

Hints

Hint 1

What happens when you INNER JOIN on a column that contains NULL values? Trace what `pr.store_id = s.store_id` evaluates to for every national promotion row.

Hint 2

The timestamps are stored in UTC but the business wants Eastern dates. Think about what `DATE(order_ts_utc)` returns for an order placed at 11 PM Eastern time — and what that means at promo window boundaries.

Hint 3

The promotions table is being used as a regular JOIN, which can multiply rows when more than one promo row matches a product. Consider whether you actually need rows from that table, or just a yes/no eligibility check.