Interview questions · Home Depot
Debug row loss after SQL joins
This question evaluates SQL data manipulation and JOIN logic skills, including temporal and timezone-aware date handling and aggregation, within the Data Manipulation (SQL/Python) domain and primarily tests practical application-level competency.
You are reviewing a buggy SQL query used by a retail analytics team at a home-improvement company. The query is supposed to return daily promo sales for mulch products during the Spring Mulch Promo.
Tables
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;
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.
Community answers
Nothing here yet.
Join in on CSWork →