cswork / libraryOpen practice workspace

Coding questions · DoorDash

Find orders from bottom-quartile revenue restaurants

This question evaluates proficiency in SQL data manipulation, including time-windowed aggregation, joins, filtering by status and date, and percentile-based ranking of restaurant revenue using completed orders.

SQL Question

You want to identify orders coming from restaurants whose total revenue is in the bottom 25th percentile.

Assume the following tables:

restaurants

  • restaurant_id (BIGINT, PK)
  • name (VARCHAR)
  • market_id (BIGINT)

orders

  • order_id (BIGINT, PK)
  • restaurant_id (BIGINT, FK → restaurants.restaurant_id)
  • customer_id (BIGINT)
  • order_total (DECIMAL(10,2)) — revenue for this order (exclude tips)
  • created_at (TIMESTAMP)
  • status (VARCHAR) — e.g., 'completed', 'canceled'

Task

Write a SQL query to return orders from restaurants whose total completed-order revenue is in the bottom 25% among all restaurants over a specified analysis window.

Requirements

  • Use an analysis window of the last 30 days relative to CURRENT_DATE.
  • Consider only status = 'completed' orders.
  • Define restaurant revenue as SUM(order_total) over the window.
  • Compute the 25th percentile of restaurant revenue across restaurants with ≥1 completed order in the window.

Output columns

  • order_id
  • restaurant_id
  • order_total
  • created_at
  • restaurant_revenue_30d

State any assumptions (e.g., percentile function availability in your SQL dialect).

Find orders from bottom-quartile revenue restaurants

You are given DoorDash-style order data. Define a restaurant's revenue as the sum of total_amount from DELIVERED orders during 2025-05-01 to 2025-05-31 (inclusive).

Task:

  1. Compute each restaurant's revenue for 2025-05.
  2. Rank restaurants into revenue quartiles using NTILE(4) ordered by revenue ascending (lowest revenue = quartile 1).
  3. Return all DELIVERED orders in 2025-05 that belong to restaurants in the bottom revenue quartile (quartile 1).

Output columns: order_id, restaurant_id, restaurant_name, order_date, total_amount, restaurant_may_revenue.

Note: Only DELIVERED orders count toward revenue and should appear in the output.

Table restaurants

columntypeconstraints
restaurant_idINTPRIMARY KEY, NOT NULL
restaurant_nameVARCHAR(100)NOT NULL

Sample data:

restaurant_idrestaurant_name
1Sushi Zen
2Taco Town
3Pasta Palace
4Burger Barn
5Salad Stop
6Curry Corner
7Pizza Plaza
8BBQ House

Table orders

columntypeconstraints
order_idINTPRIMARY KEY, NOT NULL
restaurant_idINTNOT NULL
customer_idINTNOT NULL
order_dateDATENOT NULL
statusVARCHAR(20)NOT NULL
total_amountDECIMAL(10,2)NOT NULL

Sample data:

customer_idorder_dateorder_idrestaurant_idstatustotal_amount
10012025-05-021011DELIVERED200
10022025-05-101021DELIVERED150
10032025-05-281031DELIVERED150
10042025-04-201041DELIVERED80
11012025-05-052012DELIVERED60
11022025-05-182022DELIVERED60
11032025-05-252032CANCELED40
11042025-04-152042DELIVERED100
12012025-05-033013DELIVERED300
12022025-05-113023DELIVERED300
12032025-05-293033DELIVERED300
13012025-05-074014DELIVERED100
13022025-05-194024DELIVERED200
13032025-04-094034DELIVERED50
14012025-05-035015DELIVERED20
14022025-05-205025DELIVERED30
15012025-05-046016DELIVERED200
15022025-05-126026DELIVERED250
15032025-05-266036DELIVERED250
16012025-05-097017DELIVERED120
16022025-05-217027DELIVERED130
17012025-05-068018DELIVERED400
17022025-05-168028DELIVERED300
17032025-05-308038DELIVERED300

Expected output

Returns all delivered May 2025 orders that come from restaurants in the bottom revenue quartile (using NTILE(4) over restaurant May revenue ascending). Columns: order_id, restaurant_id, restaurant_name, order_date, total_amount, restaurant_may_revenue.

order_dateorder_idrestaurant_idrestaurant_may_revenuerestaurant_nametotal_amount
2025-05-03501550Salad Stop20
2025-05-052012120Taco Town60
2025-05-182022120Taco Town60
2025-05-20502550Salad Stop30

Hints

  1. First aggregate delivered order revenue per restaurant within 2025-05-01 to 2025-05-31.
  2. Use NTILE(4) over the restaurant revenue ordered ascending to identify the bottom quartile, then join back to orders.