Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Segment Orders by Delivery Outcome

Easy
SQL & Data ManipulationData WranglingCase WhenAggregationsAsked 1 times

Problem

SwiftCart wants a simple delivery performance breakdown from its orders table. Write a PostgreSQL query that uses a CASE statement to segment each order into a delivery outcome bucket, then returns the number of orders in each bucket.

Requirements

  1. Create a derived column called delivery_outcome using these rules:
    • Delivered when delivered_at is not null and delivered_at <= promised_date
    • Delivered Late when delivered_at is not null and delivered_at > promised_date
    • Cancelled when status = 'cancelled'
    • In Progress for all other rows
  2. Return delivery_outcome and order_count
  3. Group by the derived outcome and sort by order_count descending, then delivery_outcome ascending

Table Definition

column_nametypedescription
order_idINTPrimary key for the order
customer_nameVARCHAR(100)Customer name
statusVARCHAR(20)Current order status
order_dateDATEDate the order was placed
promised_dateDATEExpected delivery date
delivered_atDATEActual delivery date, null if not delivered

Schema

orders
ColumnTypeDescription
order_idPKINTPrimary key for the order
customer_nameVARCHAR(100)Customer name
statusVARCHAR(20)Current order status such as delivered, shipped, processing, or cancelled
order_dateDATEDate the order was placed
promised_dateDATEExpected delivery date
delivered_atDATEActual delivery date, null if not delivered

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
SELECT ...
FROM ...
JOIN ... ON ...
GROUP BY ...
HAVING ...
ORDER BY ... DESC;
Sign up to unlock solutions
Instacart Operations Manager Interview Questions
Next questions
InstacartAnalyze Delivery Performance with SubqueriesMediumVanguardBucket Users by Order CountEasyInstacartModel Delivery Times by Weather ConditionMedium
PostgreSQL