Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Segment Orders by Delivery Outcome

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Segment Orders by Delivery Outcome. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

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

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
Tablesorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results