Find the top 5 shops by revenue for orders placed from February 1 to March 2, 2024

Quick Overview

Write a SQL query that ranks shops by total line-item revenue from orders placed between February 1 and March 2, 2024, and returns the top five with ties broken by shop id. It tests multi-table joins, half-open timestamp ranges, revenue aggregation and deterministic top-N ordering.

Find the top 5 shops by revenue for orders placed from February 1 to March 2, 2024

Company: Etsy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Find the top 5 shops by total revenue from orders with `created_at` on or after `2024-02-01 00:00:00` and strictly before `2024-03-03 00:00:00`. Revenue is the sum of `quantity * unit_price` over all items of qualifying orders. Return `shop_id`, `shop_name` and `total_revenue` (rounded to 2 decimals), sorted by `total_revenue` descending, ties by `shop_id` ascending. ```hint Test the edges Decide by hand whether an order placed at exactly 2024-03-03 00:00:00 counts, and whether one placed at 2024-03-02 23:59:59 does, then make sure your filter agrees. ``` ### Input Table(s) `shops`: one row per shop. Primary key `shop_id`. | column | type | meaning | |---|---|---| | shop_id | INT | Shop identifier | | shop_name | VARCHAR | Shop display name | `orders`: one row per order. Primary key `order_id`. `shop_id` references `shops.shop_id`. | column | type | meaning | |---|---|---| | order_id | INT | Order identifier | | shop_id | INT | Shop that received the order | | created_at | TIMESTAMP | When the order was placed | `order_items`: one row per line item. Primary key `order_item_id`. `order_id` references `orders.order_id`. | column | type | meaning | |---|---|---| | order_item_id | INT | Line item identifier | | order_id | INT | Order the item belongs to | | quantity | INT | Units of the item, at least 1 | | unit_price | NUMERIC(10,2) | Price per unit, at least 0 | ### Output Contract | column | meaning | |---|---| | shop_id | Shop identifier | | shop_name | Shop name from `shops` | | total_revenue | Sum of `quantity * unit_price` over the items of the shop's qualifying orders, rounded to 2 decimals | Sort by `total_revenue` descending, ties by `shop_id` ascending, and return at most 5 rows. `shop_id` is unique, so the order and the cut at 5 rows are deterministic. ### Example `shops` | shop_id | shop_name | |---|---| | 1 | Maple Ceramics | | 2 | Loom Studio | | 3 | Birch Prints | | 4 | Copper Kettle | | 5 | Fern Candles | | 6 | Oak Toys | `orders` | order_id | shop_id | created_at | |---|---|---| | 11 | 1 | 2024-02-01 00:00:00 | | 12 | 1 | 2024-01-31 23:59:59 | | 13 | 2 | 2024-02-15 10:30:00 | | 14 | 2 | 2024-02-28 16:45:00 | | 15 | 3 | 2024-03-02 23:59:59 | | 16 | 3 | 2024-03-03 00:00:00 | | 17 | 4 | 2024-02-20 08:00:00 | | 18 | 5 | 2024-02-29 12:00:00 | | 19 | 6 | 2024-02-10 09:15:00 | `order_items` | order_item_id | order_id | quantity | unit_price | |---|---|---|---| | 101 | 11 | 2 | 25.00 | | 102 | 11 | 1 | 12.50 | | 103 | 12 | 10 | 30.00 | | 104 | 13 | 3 | 15.99 | | 105 | 14 | 1 | 40.00 | | 106 | 15 | 3 | 20.00 | | 107 | 16 | 5 | 100.00 | | 108 | 17 | 2 | 31.25 | | 109 | 18 | 1 | 99.99 | | 110 | 19 | 4 | 15.00 | Expected result: | shop_id | shop_name | total_revenue | |---|---|---| | 5 | Fern Candles | 99.99 | | 2 | Loom Studio | 87.97 | | 1 | Maple Ceramics | 62.50 | | 4 | Copper Kettle | 62.50 | | 3 | Birch Prints | 60.00 | Order 12 (before February 1) and order 16 (exactly at the upper bound) are excluded, so shop 1 keeps only 62.50 and shop 3 keeps only 60.00. Shops 1 and 4 tie at 62.50, and shops 3 and 6 tie at 60.00 for fifth place; the smaller `shop_id` wins each tie, so shop 6 is cut. ### Constraints and Clarifications - `shops` has 1 to 10,000 rows, `orders` 1 to 100,000 rows and `order_items` 1 to 1,000,000 rows. - `quantity >= 1` and `unit_price >= 0`. Neither column, nor `created_at`, contains NULLs. - All foreign keys reference valid rows. - The window includes `2024-02-01 00:00:00` and excludes `2024-03-03 00:00:00`, so it runs through 2024-03-02 23:59:59. Compare `created_at` as stored, with no time zone conversion. - A shop appears only if at least one of its orders in the window has at least one item. A shop whose qualifying items all have `unit_price = 0` appears with 0.00. If fewer than 5 shops qualify, return all of them.

Overview: Write a SQL query that ranks shops by total line-item revenue from orders placed between February 1 and March 2, 2024, and returns the top five with ties broken by shop id. It tests multi-table joins, half-open timestamp ranges, revenue aggregation and deterministic top-N ordering.

Etsy logo
Etsy
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Find the top 5 shops by total revenue from orders with created_at on or after 2024-02-01 00:00:00 and strictly before 2024-03-03 00:00:00. Revenue is the sum of quantity * unit_price over all items of qualifying orders. Return shop_id, shop_name and total_revenue (rounded to 2 decimals), sorted by total_revenue descending, ties by shop_id ascending.

Input Table(s)

shops: one row per shop. Primary key shop_id.

columntypemeaning
shop_idINTShop identifier
shop_nameVARCHARShop display name

orders: one row per order. Primary key order_id. shop_id references shops.shop_id.

columntypemeaning
order_idINTOrder identifier
shop_idINTShop that received the order
created_atTIMESTAMPWhen the order was placed

order_items: one row per line item. Primary key order_item_id. order_id references orders.order_id.

columntypemeaning
order_item_idINTLine item identifier
order_idINTOrder the item belongs to
quantityINTUnits of the item, at least 1
unit_priceNUMERIC(10,2)Price per unit, at least 0

Output Contract

columnmeaning
shop_idShop identifier
shop_nameShop name from shops
total_revenueSum of quantity * unit_price over the items of the shop's qualifying orders, rounded to 2 decimals

Sort by total_revenue descending, ties by shop_id ascending, and return at most 5 rows. shop_id is unique, so the order and the cut at 5 rows are deterministic.

Example

shops

shop_idshop_name
1Maple Ceramics
2Loom Studio
3Birch Prints
4Copper Kettle
5Fern Candles
6Oak Toys

orders

order_idshop_idcreated_at
1112024-02-01 00:00:00
1212024-01-31 23:59:59
1322024-02-15 10:30:00
1422024-02-28 16:45:00
1532024-03-02 23:59:59
1632024-03-03 00:00:00
1742024-02-20 08:00:00
1852024-02-29 12:00:00
1962024-02-10 09:15:00

order_items

order_item_idorder_idquantityunit_price
10111225.00
10211112.50
103121030.00
10413315.99
10514140.00
10615320.00
107165100.00
10817231.25
10918199.99
11019415.00

Expected result:

shop_idshop_nametotal_revenue
5Fern Candles99.99
2Loom Studio87.97
1Maple Ceramics62.50
4Copper Kettle62.50
3Birch Prints60.00

Order 12 (before February 1) and order 16 (exactly at the upper bound) are excluded, so shop 1 keeps only 62.50 and shop 3 keeps only 60.00. Shops 1 and 4 tie at 62.50, and shops 3 and 6 tie at 60.00 for fifth place; the smaller shop_id wins each tie, so shop 6 is cut.

Constraints and Clarifications

  • shops has 1 to 10,000 rows, orders 1 to 100,000 rows and order_items 1 to 1,000,000 rows.
  • quantity >= 1 and unit_price >= 0 . Neither column, nor created_at , contains NULLs.
  • All foreign keys reference valid rows.
  • The window includes 2024-02-01 00:00:00 and excludes 2024-03-03 00:00:00 , so it runs through 2024-03-02 23:59:59. Compare created_at as stored, with no time zone conversion.
  • A shop appears only if at least one of its orders in the window has at least one item. A shop whose qualifying items all have unit_price = 0 appears with 0.00. If fewer than 5 shops qualify, return all of them.