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.
orders
order_id
BIGINT
customer_id
BIGINT
store_id
INT
order_ts_utc
TIMESTAMP
order_status
VARCHAR
order_items
order_id
BIGINT
line_id
INT
product_id
INT
quantity
INT
unit_price
NUMERIC(10,2)
products
product_id
INT
product_name
VARCHAR
category
VARCHAR
promotions
promo_id
INT
promo_name
VARCHAR
product_id
INT
store_id
INT NULL
start_date
DATE
end_date
DATE
is_national
BOOLEAN
stores
store_id
INT
region
VARCHAR
timezone
VARCHAR
order_status = 'DELIVERED'
.
category = 'Mulch'
.
promotions.store_id
is populated), or
promotions.store_id
is
NULL
and applies to all stores).
Return one row per:
order_date_et
DATE
store_id
INT
product_id
INT
with:
units_sold
gross_revenue
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;