Practice Free
MediumAggregations

Gross Merchandise Value & Refund 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.

Solve in Workbench

Problem Statement

Analyze order transactions in `orders` grouped by `category`. Calculate: 1. `category` 2. `total_orders`: Count of all orders in that category 3. `total_gmv`: Sum of `order_amount` 4. `refund_rate_pct`: Percentage of total order amount that was refunded, calculated as: `ROUND(100.0 * SUM(refunded_amount) / SUM(order_amount), 2)` Order results by `refund_rate_pct` in **descending** order.

Why Technical Interviewers Ask This

E-commerce merchants frequently suffer from high return rates in certain product categories. The VP of Operations wants to audit total gross sales (GMV) and calculate the percentage of total sales refunded per category.

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 SQL
SELECT 
        category,
        COUNT(*) AS total_orders,
        SUM(order_amount) AS total_gmv,
        ROUND(100.0 * SUM(refunded_amount) / SUM(order_amount), 2) AS refund_rate_pct
      FROM orders
      GROUP BY category
      ORDER BY refund_rate_pct DESC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Summary table of category, total_orders, total_gmv, and refund_rate_pct sorted by highest refund rate first.

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.

More FAANG SQL Interview Questions

View all questions