Cancellation split by restaurant

reshaping

Cancellation split by restaurant

Swiggy SQL Interview Question

A restaurant that accepts an order and then cancels it costs Swiggy more than the order, because the app has already given the customer a delivery time. Partner operations calls the worst offenders and drops them down the listings until they improve, so they need to know which restaurants are canceling and how much of their volume it comes to.

Return the restaurant_name, the number of delivered orders as delivered_orders, the number of canceled ones as canceled_orders, and the canceled share of that restaurant's orders as canceled_share, as a percentage rounded to 2 decimal places. A restaurant that has never canceled an order shows 0.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

swiggy_ordersTable32 rows

Column NameType
order_idBIGINT
cityVARCHAR
restaurant_idBIGINT
placed_atTIMESTAMP
delivery_minutesBIGINT
order_totalDOUBLE
statusVARCHAR

swiggy_restaurantsTable9 rows

Column NameType
restaurant_idBIGINT
restaurant_nameVARCHAR
cityVARCHAR
cuisineVARCHAR
onboarded_onDATE

swiggy_ordersExample Input

order_idcityrestaurant_idplaced_atdelivery_minutesorder_totalstatus
4001Bengaluru12024-07-01 12:10:0032540delivered
4002Bengaluru22024-07-01 13:05:0041320delivered
4003Bengaluru12024-07-01 20:15:0028460delivered
4004Bengaluru32024-07-02 12:40:00NULL275canceled
4005Bengaluru22024-07-02 19:20:0036680delivered
4006Bengaluru12024-07-03 13:15:0025390delivered
4007Bengaluru32024-07-03 20:05:0052510delivered
4011Hyderabad42024-07-01 12:30:0038410delivered
4012Hyderabad52024-07-01 19:50:0044295delivered
4013Hyderabad42024-07-02 13:20:0031530delivered
4014Hyderabad62024-07-02 20:40:0058700delivered
4015Hyderabad52024-07-03 12:55:00NULL260canceled
4016Hyderabad62024-07-03 19:35:0049480delivered

swiggy_restaurantsExample Input

restaurant_idrestaurant_namecitycuisineonboarded_on
1Empire GrillBengaluruNorth Indian2022-03-14
2Truffles CornerBengaluruContinental2021-11-02
3Meghana Biryani HouseBengaluruBiryani2023-01-20
4Paradise SpiceHyderabadBiryani2020-08-11
5Chutneys ExpressHyderabadSouth Indian2022-06-30
6Shah Ghouse CafeHyderabadNorth Indian2021-04-05

Example Output

restaurant_namedelivered_orderscanceled_orderscanceled_share
Chutneys Express1150
Empire Grill300
Meghana Biryani House1150
Paradise Spice200
Shah Ghouse Cafe200
Truffles Corner200

Explanation

Meghana Biryani House took two orders in the example, delivered one and canceled one, so half of what it accepted fell through: 50. Empire Grill delivered three and canceled none, and still appears, on a canceled share of 0 rather than dropping out of the report.

The example above is a small slice of the data. Your query runs against the full tables.