← All SQL interview questions

Courier scorecard with conditional counts

MediumCASE StatementsFlipkartEXL

A logistics lead wants a one-row-per-courier scorecard instead of a long list of parcel statuses.

From parcel_events, return courier, delivered, returned, in_transit (the count of parcels in each status) and delivery_rate_pct (delivered parcels as a percentage of all the courier's parcels, 1 decimal). Order by delivery_rate_pct descending, then courier.

Sample data

Table: parcel_events

20 row(s) · showing first 8

parcel_idINTcourierTEXTstatusTEXT
1SwiftShipDELIVERED
2SwiftShipDELIVERED
3SwiftShipDELIVERED
4SwiftShipRETURNED
5SwiftShipIN_TRANSIT
6RoadRunnerDELIVERED
7RoadRunnerDELIVERED
8RoadRunnerRETURNED