Average delivery earnings by day of week and hour of day in SQL

Quick Overview

Write a SQL query that averages delivery earnings for each combination of day of the week and hour of the day, with days in calendar order starting on Sunday. It tests date part extraction, grouping on derived values, rounding, and sorting days by number instead of name.

Average delivery earnings by day of week and hour of day in SQL

Company: DoorDash

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `deliveries` table records each delivery a dasher completed, when it happened, and what the dasher earned for it. Write a query that returns the average `earnings` for each combination of day of the week and hour of the day, which shows when deliveries pay the most. ### Input Table(s) Table: `deliveries` | Column | Type | Meaning | |---|---|---| | delivery_id | INT | Delivery identifier | | dasher_id | INT | Dasher who made the delivery, never NULL | | delivery_time | TIMESTAMP | When the delivery happened, never NULL | | earnings | DECIMAL(8,2) | What the dasher earned, never NULL and never negative | Primary key: `delivery_id`. ### Output Contract | Column | Meaning | |---|---| | day_of_week | Full English day name with no padding: 'Sunday', 'Monday', ..., 'Saturday' | | hour_of_day | Hour of `delivery_time` as an integer from 0 to 23 | | avg_earnings | Average `earnings` of the deliveries in that day and hour, rounded to 2 decimals | - One row per (day of week, hour of day) combination that has at least one delivery. - Order by day of week in calendar order with Sunday first (Sunday, Monday, ..., Saturday), then `hour_of_day` ascending. Each (day, hour) pair appears once, so the order is fully determined. ### Example Input `deliveries`: | delivery_id | dasher_id | delivery_time | earnings | |---|---|---|---| | 1 | 101 | 2024-06-02 18:05:00 | 8.50 | | 2 | 102 | 2024-06-02 18:40:00 | 11.25 | | 3 | 101 | 2024-06-09 18:15:00 | 9.00 | | 4 | 103 | 2024-06-02 09:30:00 | 6.00 | | 5 | 102 | 2024-06-03 12:10:00 | 7.40 | | 6 | 101 | 2024-06-03 12:55:00 | 5.10 | | 7 | 103 | 2024-06-08 00:20:00 | 12.00 | | 8 | 102 | 2024-06-08 23:59:00 | 10.00 | Expected output: | day_of_week | hour_of_day | avg_earnings | |---|---|---| | Sunday | 9 | 6.00 | | Sunday | 18 | 9.58 | | Monday | 12 | 6.25 | | Saturday | 0 | 12.00 | | Saturday | 23 | 10.00 | June 2 and June 9, 2024 are both Sundays, so deliveries 1, 2 and 3 share the Sunday 18:00 group even though they fall in different weeks: (8.50 + 11.25 + 9.00) / 3 = 9.5833, which rounds to 9.58. Deliveries 5 and 6 average 6.25 for Monday at hour 12. June 8 is a Saturday, and its deliveries at 00:20 and 23:59 land in hours 0 and 23. Hour 9 sorts before hour 18 because hours are compared as numbers. ### Constraints and Clarifications - Use `delivery_time` as stored, with no time zone conversion. - Combinations with no deliveries are left out. Do not fill in rows with an average of 0. - Days are grouped by weekday across all weeks in the table, not by calendar date. - Average over all deliveries in the group, then round once. Halves round away from zero, as `ROUND` does on a numeric value. - `deliveries` has at most 10,000 rows. ```hint Ordering the days Day names sort alphabetically, which would put Friday first. Find a numeric day value to order by. ```

Overview: Write a SQL query that averages delivery earnings for each combination of day of the week and hour of the day, with days in calendar order starting on Sunday. It tests date part extraction, grouping on derived values, rounding, and sorting days by number instead of name.

DoorDash logo
DoorDash
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The deliveries table records each delivery a dasher completed, when it happened, and what the dasher earned for it. Write a query that returns the average earnings for each combination of day of the week and hour of the day, which shows when deliveries pay the most.

Input Table(s)

Table: deliveries

ColumnTypeMeaning
delivery_idINTDelivery identifier
dasher_idINTDasher who made the delivery, never NULL
delivery_timeTIMESTAMPWhen the delivery happened, never NULL
earningsDECIMAL(8,2)What the dasher earned, never NULL and never negative

Primary key: delivery_id.

Output Contract

ColumnMeaning
day_of_weekFull English day name with no padding: 'Sunday', 'Monday', ..., 'Saturday'
hour_of_dayHour of delivery_time as an integer from 0 to 23
avg_earningsAverage earnings of the deliveries in that day and hour, rounded to 2 decimals
  • One row per (day of week, hour of day) combination that has at least one delivery.
  • Order by day of week in calendar order with Sunday first (Sunday, Monday, ..., Saturday), then hour_of_day ascending. Each (day, hour) pair appears once, so the order is fully determined.

Example

Input deliveries:

delivery_iddasher_iddelivery_timeearnings
11012024-06-02 18:05:008.50
21022024-06-02 18:40:0011.25
31012024-06-09 18:15:009.00
41032024-06-02 09:30:006.00
51022024-06-03 12:10:007.40
61012024-06-03 12:55:005.10
71032024-06-08 00:20:0012.00
81022024-06-08 23:59:0010.00

Expected output:

day_of_weekhour_of_dayavg_earnings
Sunday96.00
Sunday189.58
Monday126.25
Saturday012.00
Saturday2310.00

June 2 and June 9, 2024 are both Sundays, so deliveries 1, 2 and 3 share the Sunday 18:00 group even though they fall in different weeks: (8.50 + 11.25 + 9.00) / 3 = 9.5833, which rounds to 9.58. Deliveries 5 and 6 average 6.25 for Monday at hour 12. June 8 is a Saturday, and its deliveries at 00:20 and 23:59 land in hours 0 and 23. Hour 9 sorts before hour 18 because hours are compared as numbers.

Constraints and Clarifications

  • Use delivery_time as stored, with no time zone conversion.
  • Combinations with no deliveries are left out. Do not fill in rows with an average of 0.
  • Days are grouped by weekday across all weeks in the table, not by calendar date.
  • Average over all deliveries in the group, then round once. Halves round away from zero, as ROUND does on a numeric value.
  • deliveries has at most 10,000 rows.