Quick Overview

Write a SQL query that finds each customer's first order in a food delivery table and returns the percentage of those first orders whose preferred delivery date equals the order date, rounded to two decimals. It tests per-group minimums, filtering to one row per customer, and numeric division in PostgreSQL.

Compute the percentage of immediate deliveries among customers' first orders

Company: Dropbox

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A food delivery service stores one row per order in the `Delivery` table. An order is immediate when `customer_pref_delivery_date` equals `order_date`; otherwise it is scheduled. A customer's first order is the one with the earliest `order_date`. Write a query that returns `immediate_percentage`: the percentage of immediate orders among all customers' first orders, rounded to 2 decimal places. ```hint Shrink the table first Reduce `Delivery` to exactly one row per customer before you compute any percentage. ``` ### Input Table(s) Table `Delivery` | Column | Type | Meaning | |---|---|---| | delivery_id | INT | Id of the order | | customer_id | INT | Customer who placed the order (NOT NULL) | | order_date | DATE | Date the order was placed (NOT NULL) | | customer_pref_delivery_date | DATE | Delivery date the customer asked for (NOT NULL) | Primary key: `delivery_id`. ### Output Contract - Exactly one row with one column, `immediate_percentage`. - The value is 100 times the number of customers whose first order is immediate divided by the number of customers, rounded to 2 decimal places (for example 33.33, or 50.00 when exactly half the first orders are immediate). - The result is a single row, so no ordering is needed. ### Example Input `Delivery`: | delivery_id | customer_id | order_date | customer_pref_delivery_date | |---|---|---|---| | 1 | 201 | 2024-05-01 | 2024-05-01 | | 2 | 202 | 2024-05-02 | 2024-05-04 | | 3 | 201 | 2024-05-03 | 2024-05-05 | | 4 | 203 | 2024-05-03 | 2024-05-03 | | 5 | 202 | 2024-05-06 | 2024-05-06 | | 6 | 204 | 2024-05-07 | 2024-05-08 | | 7 | 205 | 2024-05-12 | 2024-05-12 | | 8 | 205 | 2024-05-08 | 2024-05-10 | | 9 | 206 | 2024-05-09 | 2024-05-11 | Expected output: | immediate_percentage | |---| | 33.33 | The first orders are rows 1, 2, 4, 6, 8 and 9, one for each of the 6 customers. Customer 205's first order is row 8, because 2024-05-08 is earlier than 2024-05-12, even though row 7 has the smaller `delivery_id`. Rows 1 and 4 are the only immediate first orders, so the answer is 2 out of 6, which is 33.333... percent and rounds to 33.33. Rows 5 and 7 are immediate but are not first orders, so they do not count. ### Constraints and Clarifications - `Delivery` has between 1 and 10,000 rows, so there is always at least one customer. - No column is NULL. - `delivery_id` is unique, so each row is one order. - Each customer has exactly one order on their earliest `order_date`, so every customer has exactly one first order. - `customer_pref_delivery_date` is never earlier than `order_date`. - Only first orders count. A customer's later orders are ignored, even when they are immediate. - When the exact percentage ends in 5 at the third decimal place, round away from zero, which is what PostgreSQL `ROUND` does on a `numeric` value.

Overview: Write a SQL query that finds each customer's first order in a food delivery table and returns the percentage of those first orders whose preferred delivery date equals the order date, rounded to two decimals. It tests per-group minimums, filtering to one row per customer, and numeric division in PostgreSQL.

A food delivery service stores one row per order in the `delivery` table. An order is immediate when `customer_pref_delivery_date` equals `order_date`; otherwise it is scheduled. A customer's first order is the one with the earliest `order_date`. Write a query that returns `immediate_percentage`: the percentage of immediate orders among all customers' first orders, rounded to 2 decimal places. Table `delivery`: - `delivery_id` (INTEGER, primary key): id of the order. - `customer_id` (INTEGER, NOT NULL): customer who placed the order. - `order_date` (DATE, NOT NULL): date the order was placed. - `customer_pref_delivery_date` (DATE, NOT NULL): delivery date the customer asked for. Output: - Exactly one row with one column, `immediate_percentage`. - The value is 100 times the number of customers whose first order is immediate divided by the number of customers, rounded to 2 decimal places (for example 33.33, or 50.00 when exactly half the first orders are immediate). - The result is a single row, so no ordering is needed. Constraints and clarifications: - `delivery` has between 1 and 10,000 rows, so there is always at least one customer. - No column is NULL. - `delivery_id` is unique, so each row is one order. - Each customer has exactly one order on their earliest `order_date`, so every customer has exactly one first order. - `customer_pref_delivery_date` is never earlier than `order_date`. - Only first orders count. A customer's later orders are ignored, even when they are immediate. - When the exact percentage ends in 5 at the third decimal place, round away from zero, which is what PostgreSQL `ROUND` does on a `numeric` value. With the sample rows, the first orders are delivery_id 1, 2, 4, 6, 8 and 9, one for each of the 6 customers. Customer 205's first order is delivery_id 8, because 2024-05-08 is earlier than 2024-05-12, even though delivery_id 7 is smaller. Orders 1 and 4 are the only immediate first orders, so the answer is 2 out of 6, which is 33.333... percent and rounds to 33.33. Orders 5 and 7 are immediate later orders, so they do not count.

Tables

delivery(delivery_id INTEGER, customer_id INTEGER, order_date DATE, customer_pref_delivery_date DATE)

Hints

  1. Reduce `delivery` to exactly one row per customer before you compute any percentage.
  2. The earliest `order_date` decides a customer's first order, even when a later order has a smaller `delivery_id`.
DropboxData ScientistmediumData Manipulation (SQL/Python)

Immediate Delivery Percentage Among First Orders

A food delivery service stores one row per order in the delivery table. An order is immediate when customer_pref_delivery_date equals order_date; otherwise it is scheduled. A customer's first order is the one with the earliest order_date. Write a query that returns immediate_percentage: the percentage of immediate orders among all customers' first orders, rounded to 2 decimal places.

Table delivery:

  • delivery_id (INTEGER, primary key): id of the order.
  • customer_id (INTEGER, NOT NULL): customer who placed the order.
  • order_date (DATE, NOT NULL): date the order was placed.
  • customer_pref_delivery_date (DATE, NOT NULL): delivery date the customer asked for.

Output:

  • Exactly one row with one column, immediate_percentage.
  • The value is 100 times the number of customers whose first order is immediate divided by the number of customers, rounded to 2 decimal places (for example 33.33, or 50.00 when exactly half the first orders are immediate).
  • The result is a single row, so no ordering is needed.

Constraints and clarifications:

  • delivery has between 1 and 10,000 rows, so there is always at least one customer.
  • No column is NULL.
  • delivery_id is unique, so each row is one order.
  • Each customer has exactly one order on their earliest order_date, so every customer has exactly one first order.
  • customer_pref_delivery_date is never earlier than order_date.
  • Only first orders count. A customer's later orders are ignored, even when they are immediate.
  • When the exact percentage ends in 5 at the third decimal place, round away from zero, which is what PostgreSQL ROUND does on a numeric value.

With the sample rows, the first orders are delivery_id 1, 2, 4, 6, 8 and 9, one for each of the 6 customers. Customer 205's first order is delivery_id 8, because 2024-05-08 is earlier than 2024-05-12, even though delivery_id 7 is smaller. Orders 1 and 4 are the only immediate first orders, so the answer is 2 out of 6, which is 33.333... percent and rounds to 33.33. Orders 5 and 7 are immediate later orders, so they do not count.

Expected Output

Tables

delivery
delivery
ColumnType
delivery_idINTEGER
customer_idINTEGER
order_dateDATE
customer_pref_delivery_dateDATE

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