Trip stats by city

easySQL~10 min · 4 tests · ridelog

Ops wants a one-line health check per city: how busy it is, how often riders get let down, and what a finished trip pays.

A trip’s status is either 'completed' or 'cancelled'. Cancelled trips have NULL fare, ended_at and distance_km.

Return one row per city with:

column type meaning
city text the city
trips integer every trip in that city, cancelled or not
cancelled integer trips with status 'cancelled'
cancel_pct decimal cancelled / trips * 100, rounded to 1 decimal
avg_fare decimal average fare of completed trips, rounded to 2 decimals

A cancelled trip has no fare, so it must not drag the average down. If a city has no completed trips, avg_fare is NULL.

Row order doesn’t matter.

Tables: trips.

Learn the ideaRead the lesson →
solution.sqlDuckDB · ridelog

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run