Debug row loss after SQL joins
Problem Description
You are tasked with reviewing and correcting a buggy SQL query used by a retail analytics team at a home-improvement company. The objective of the query is to calculate daily promotional sales for mulch products specifically during the "Spring Mulch Promo".
Data Schema
orders
order_idBIGINTcustomer_idBIGINTstore_idINTorder_ts_utcTIMESTAMPorder_statusVARCHAR
order_items
order_idBIGINTline_idINTproduct_idINTquantityINTunit_priceNUMERIC(10,2)
products
product_idINTproduct_nameVARCHARcategoryVARCHAR
promotions
promo_idINTpromo_nameVARCHARproduct_idINTstore_idINT NULLstart_dateDATEend_dateDATEis_nationalBOOLEAN
stores
store_idINTregionVARCHARtimezoneVARCHAR
Business Rules
- Analyze orders from 2025-03-01 to 2025-05-31.
- Only include orders with
order_status = 'DELIVERED'. - Only include products where
category = 'Mulch'. - Reporting should use America/New_York as the business timezone.
- A promotion can be either:
- store-specific (
promotions.store_idis populated), or - national (
promotions.store_idisNULLand applies to all stores).
- store-specific (
Expected Output
Return one row per:
order_date_etDATEstore_idINTproduct_idINT
with:
units_soldgross_revenue
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 = 'DELIVERED'
AND p.category = 'Mulch'
AND pr.promo_name = 'Spring Mulch 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
- Explain why this query may return far fewer rows than expected after the joins.
- Identify the logical bugs, especially around join conditions and date handling.
- Rewrite the query so it correctly captures mulch orders that occurred during the promotion window, including both store-specific and national promotions.
AI interview prep
Core dilemma
Correctly associating orders with promotions that have varying scope (national vs. store-specific) and temporal constraints, while accurately handling timezone conversions, all within the constraints of SQL join logic.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Debug row loss after SQL joins
Problem Description
You are tasked with reviewing and correcting a buggy SQL query used by a retail analytics team at a home-improvement company. The objective of the query is to calculate daily promotional sales for mulch products specifically during the "Spring Mulch Promo".
Data Schema
orders
order_idBIGINTcustomer_idBIGINTstore_idINTorder_ts_utcTIMESTAMPorder_statusVARCHAR
order_items
order_idBIGINTline_idINTproduct_idINTquantityINTunit_priceNUMERIC(10,2)
products
product_idINTproduct_nameVARCHARcategoryVARCHAR
promotions
promo_idINTpromo_nameVARCHARproduct_idINTstore_idINT NULLstart_dateDATEend_dateDATEis_nationalBOOLEAN
stores
store_idINTregionVARCHARtimezoneVARCHAR
Business Rules
- Analyze orders from 2025-03-01 to 2025-05-31.
- Only include orders with
order_status = 'DELIVERED'. - Only include products where
category = 'Mulch'. - Reporting should use America/New_York as the business timezone.
- A promotion can be either:
- store-specific (
promotions.store_idis populated), or - national (
promotions.store_idisNULLand applies to all stores).
- store-specific (
Expected Output
Return one row per:
order_date_etDATEstore_idINTproduct_idINT
with:
units_soldgross_revenue
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 = 'DELIVERED'
AND p.category = 'Mulch'
AND pr.promo_name = 'Spring Mulch 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
- Explain why this query may return far fewer rows than expected after the joins.
- Identify the logical bugs, especially around join conditions and date handling.
- Rewrite the query so it correctly captures mulch orders that occurred during the promotion window, including both store-specific and national promotions.