SQL questions based on DoorDash data schema, python leet code medium
Using the DoorDash-style tables provided, write a PostgreSQL query for Seattle restaurants. Include restaurants without qualifying deliveries and consider only completed orders delivered during the first quarter of 2024.
Output
- One row per Seattle restaurant, with
restaurant_name, completed_delivery_count, and avg_delivery_minutes.
- Sort by completed delivery count descending, then restaurant name ascending.
- Return NULL for the average when no qualifying delivery has a measurable duration.
Output details
- Use
delivered_at >= TIMESTAMP '2024-01-01 00:00:00' and delivered_at < TIMESTAMP '2024-04-01 00:00:00'.
- Count only records with
order_status = 'completed' and delivery_status = 'delivered'.
- Calculate duration as elapsed minutes from
picked_up_at to delivered_at.
- Round
avg_delivery_minutes to 2 decimal places.