MediumJoins
Active Rideshare Drivers with Expired Vehicle Inspections
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
Join `rideshare_drivers` to `vehicle_inspections` on `vehicle_id`.
Filter for drivers who are currently `driver_status = 'active'` but whose `inspection_status = 'expired'`.
Return:
1. `d.driver_id`
2. `d.driver_name`
3. `v.license_plate`
4. `v.days_since_inspection`
Order results by `v.days_since_inspection` descending, then by `d.driver_id` ascending.
Why Technical Interviewers Ask This
City transportation regulators audit rideshare platforms to ensure that any driver marked 'active' is operating an inspected vehicle whose safety certification has not lapsed.
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 joins constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLSELECT
d.driver_id,
d.driver_name,
v.license_plate,
v.days_since_inspection
FROM rideshare_drivers d
INNER JOIN vehicle_inspections v ON d.vehicle_id = v.vehicle_id
WHERE d.driver_status = 'active'
AND v.inspection_status = 'expired'
ORDER BY v.days_since_inspection DESC, d.driver_id ASC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Joins. It meets the target criteria: Active drivers operating vehicles with expired safety inspection certificates.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 Joins 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.