AnalystPath

Pick the Best Courier for Each Delivery Zone

SQLMediumMid level~15 min

Problem

A food-delivery company assigns one courier to each delivery zone. A zone lists the vehicle capabilities it needs and how critical each one is (level 1 to 5). A courier is eligible for a zone only if they possess every capability the zone requires. For each eligible courier compute a fit_score that starts at 100, adds 10 for every capability where the courier's skill_level is strictly greater than the zone's needed level, and subtracts 5 for every capability where the courier's skill_level is strictly less than the needed level. For each zone return the courier with the highest fit_score; break ties by the smaller courier_id. Return zone_id, courier_id and fit_score, ordered by zone_id.

Tables

Example rows — the live problem includes the full dataset.

couriers
courier_idcapabilityskill_level
701cold_chain5
701bike3
701cash_handling4
zones
zone_idcapabilityneeded_level
40cold_chain4
40bike3
40cash_handling5

Expected output

Your answer should return 2 rows with the columns zone_id, courier_id, fit_score.

Starter code (SQL)

SELECT *
FROM couriers;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions