Compute the percentage of immediate food delivery orders

Quick Overview

Write a SQL query over a food delivery orders table that returns the share of orders whose preferred delivery date matches the order date, as a percentage rounded to two decimals. It tests conditional aggregation, numeric types in division, and rounding in PostgreSQL.

Compute the percentage of immediate food delivery orders

Company: Databricks

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

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. Write a query that returns the percentage of immediate orders in `Delivery`, rounded to 2 decimal places, as `immediate_percentage`. ```hint Watch the division In PostgreSQL, dividing one integer count by another truncates the result. Check the type of each operand before you round. ``` ### 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 immediate orders divided by the total number of orders, rounded to 2 decimal places (for example 42.86, or 50.00 when exactly half the 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 | 101 | 2024-03-01 | 2024-03-01 | | 2 | 102 | 2024-03-01 | 2024-03-03 | | 3 | 101 | 2024-03-04 | 2024-03-05 | | 4 | 103 | 2024-03-04 | 2024-03-04 | | 5 | 104 | 2024-03-06 | 2024-03-09 | | 6 | 102 | 2024-03-07 | 2024-03-07 | | 7 | 105 | 2024-03-08 | 2024-03-10 | Expected output: | immediate_percentage | |---| | 42.86 | Orders 1, 4 and 6 have matching dates, so 3 of the 7 orders are immediate. 3 divided by 7 is 0.428571..., which is 42.857... percent and rounds to 42.86. ### Constraints and Clarifications - `Delivery` has between 1 and 10,000 rows, so the total is never zero. - No column is NULL, so every order is either immediate or scheduled. - `delivery_id` is unique, so each row is one order. A customer can appear on many rows, and every one of those orders counts. - Any difference between the two dates makes the order scheduled. - 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 over a food delivery orders table that returns the share of orders whose preferred delivery date matches the order date, as a percentage rounded to two decimals. It tests conditional aggregation, numeric types in division, and rounding in PostgreSQL.

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

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. Write a query that returns the percentage of immediate orders in Delivery, rounded to 2 decimal places, as immediate_percentage.

Input Table(s)

Table Delivery

ColumnTypeMeaning
delivery_idINTId of the order
customer_idINTCustomer who placed the order (NOT NULL)
order_dateDATEDate the order was placed (NOT NULL)
customer_pref_delivery_dateDATEDelivery 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 immediate orders divided by the total number of orders, rounded to 2 decimal places (for example 42.86, or 50.00 when exactly half the orders are immediate).
  • The result is a single row, so no ordering is needed.

Example

Input Delivery:

delivery_idcustomer_idorder_datecustomer_pref_delivery_date
11012024-03-012024-03-01
21022024-03-012024-03-03
31012024-03-042024-03-05
41032024-03-042024-03-04
51042024-03-062024-03-09
61022024-03-072024-03-07
71052024-03-082024-03-10

Expected output:

immediate_percentage
42.86

Orders 1, 4 and 6 have matching dates, so 3 of the 7 orders are immediate. 3 divided by 7 is 0.428571..., which is 42.857... percent and rounds to 42.86.

Constraints and Clarifications

  • Delivery has between 1 and 10,000 rows, so the total is never zero.
  • No column is NULL, so every order is either immediate or scheduled.
  • delivery_id is unique, so each row is one order. A customer can appear on many rows, and every one of those orders counts.
  • Any difference between the two dates makes the order scheduled.
  • 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.