MediumAggregations
Driver Acceptance & Ride Cancellation Benchmarks
Frequently Asked By:AppleGoogleAmazon
Interactive In-Browser Workspace
Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.
Problem Statement
Group rides in `ride_requests` by `vehicle_tier`.
Calculate:
1. `vehicle_tier`
2. `total_requests`: Total ride requests in that tier
3. `completed_rides`: Count of rides where `status = 'completed'`
4. `completion_rate_pct`: Percentage of rides completed:
`ROUND(100.0 * SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) / COUNT(*), 1)`
Order results by `completion_rate_pct` descending.
Why Technical Interviewers Ask This
Ride reliability is essential for platform retention. The city operations manager needs to analyze completed versus canceled rides across different vehicle tiers to spot service breakdown.
Common Pitfalls & Interview Traps
Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass aggregations constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLSELECT
vehicle_tier,
COUNT(*) AS total_requests,
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) AS completed_rides,
ROUND(100.0 * SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) / COUNT(*), 1) AS completion_rate_pct
FROM ride_requests
GROUP BY vehicle_tier
ORDER BY completion_rate_pct DESC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Aggregations. It meets the target criteria: Summary of ride requests, completed rides, and completion percentage per vehicle tier.Edge Cases to Verbally Clarify with Your Interviewer
The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.
Frequently Asked Follow-Up Questions
What makes this question a favorite among Apple and Google interviewers?
Interviewers use this problem to test whether candidates write defensive, scalable Aggregations queries that run efficiently on multi-million row production databases without excessive memory usage.
How does the query optimizer execute this query?
Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.