MediumAggregations
Marketplace Merchant 48-Hour Fulfillment SLA Rate
Frequently Asked By:AmazonShopifyUber
Interactive In-Browser Workspace
Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.
Problem Statement
Group records in `merchant_orders` by `merchant_id`.
Calculate:
1. `merchant_id`
2. `total_orders`: Count of all orders
3. `on_time_orders`: Count of orders where `hours_to_dispatch <= 48`
4. `sla_compliance_pct`: Percentage of orders dispatched within 48 hours, rounded to 1 decimal place
Filter groups with `HAVING COUNT(*) >= 3` (minimum volume).
Order results by `sla_compliance_pct` DESC, then by `merchant_id` ASC.
Why Technical Interviewers Ask This
Operations leadership benchmarks third-party seller logistics by measuring the percentage of merchant orders dispatched within their contractual 48-hour service level agreement (SLA).
Common Pitfalls & Interview Traps
Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass aggregations constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLSELECT
merchant_id,
COUNT(*) AS total_orders,
COUNT(CASE WHEN hours_to_dispatch <= 48 THEN 1 END) AS on_time_orders,
ROUND(COUNT(CASE WHEN hours_to_dispatch <= 48 THEN 1 END) * 100.0 / COUNT(*), 1) AS sla_compliance_pct
FROM merchant_orders
GROUP BY merchant_id
HAVING COUNT(*) >= 3
ORDER BY sla_compliance_pct DESC, merchant_id ASC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Merchant fulfillment SLA compliance rates for active sellers.Edge Cases to Verbally Clarify with Your Interviewer
The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.
Frequently Asked Follow-Up Questions
What makes this question a favorite among Amazon and Shopify interviewers?
Interviewers use this problem to test whether candidates write defensive, scalable Aggregations queries that run efficiently on multi-million row production databases without excessive memory usage.
How does the query optimizer execute this query?
Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.