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.

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. Table `deliveries`: - `delivery_id` INTEGER: delivery identifier, the primary key. - `dasher_id` INTEGER: 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. Return these columns in this order: - `day_of_week`: the full English day name with no padding: 'Sunday', 'Monday', ..., 'Saturday'. - `hour_of_day`: the hour of `delivery_time` as an integer from 0 to 23. - `avg_earnings`: the average `earnings` of the deliveries in that day and hour, rounded to 2 decimals. Return one row per (day of week, hour of day) combination that has at least one delivery. Order the rows by day of week in calendar order with Sunday first (Sunday, Monday, ..., Saturday), then by `hour_of_day` ascending, comparing hours as numbers so hour 9 sorts before hour 18. Each (day, hour) pair appears once, so the order is fully determined. Rules: - Use `delivery_time` as stored, with no time zone conversion. - Leave out combinations with no deliveries. Do not fill in rows with an average of 0. - Group days by weekday across all weeks in the table, so deliveries on two different Sundays at the same hour fall in one Sunday group. - 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.

Tables

deliveries(delivery_id INTEGER, dasher_id INTEGER, delivery_time TIMESTAMP, earnings DECIMAL(8,2))

Hints

  1. Day names sort alphabetically, which would put Friday first. Find a numeric day value to order by.
  2. Deliveries on different calendar dates that share a weekday and an hour belong to the same group.
DoorDashData ScientisteasyData Manipulation (SQL/Python)

Average Dasher Earnings by Day of Week and Hour

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.

Table deliveries:

  • delivery_id INTEGER: delivery identifier, the primary key.
  • dasher_id INTEGER: 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.

Return these columns in this order:

  • day_of_week: the full English day name with no padding: 'Sunday', 'Monday', ..., 'Saturday'.
  • hour_of_day: the hour of delivery_time as an integer from 0 to 23.
  • avg_earnings: the average earnings of the deliveries in that day and hour, rounded to 2 decimals.

Return one row per (day of week, hour of day) combination that has at least one delivery. Order the rows by day of week in calendar order with Sunday first (Sunday, Monday, ..., Saturday), then by hour_of_day ascending, comparing hours as numbers so hour 9 sorts before hour 18. Each (day, hour) pair appears once, so the order is fully determined.

Rules:

  • Use delivery_time as stored, with no time zone conversion.
  • Leave out combinations with no deliveries. Do not fill in rows with an average of 0.
  • Group days by weekday across all weeks in the table, so deliveries on two different Sundays at the same hour fall in one Sunday group.
  • 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.

Expected Output

Tables

deliveries
deliveries
ColumnType
delivery_idINTEGER
dasher_idINTEGER
delivery_timeTIMESTAMP
earningsDECIMAL(8,2)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions