MediumJoins
Fleet Vehicle Compliance Audit with Multi-Table JOIN
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 `rides` with `drivers` and `vehicles` across three tables:
- `rides.driver_id = drivers.driver_id`
- `rides.vehicle_id = vehicles.vehicle_id`
Return:
1. `r.ride_id`
2. `d.driver_name`
3. `v.license_plate`
4. `v.model`
5. `r.fare_amount`
Filter to only include trips where `v.inspection_passed = false`.
Order results by `r.fare_amount` descending, then by `r.ride_id` ascending.
Why Technical Interviewers Ask This
Municipal transport authorities require rideshare networks to audit trips where the vehicle used was overdue for mandatory safety inspection.
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
r.ride_id,
d.driver_name,
v.license_plate,
v.model,
r.fare_amount
FROM rides r
JOIN drivers d ON r.driver_id = d.driver_id
JOIN vehicles v ON r.vehicle_id = v.vehicle_id
WHERE v.inspection_passed = false
ORDER BY r.fare_amount DESC, r.ride_id ASC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Joins. It meets the target criteria: Trips performed with uninspected vehicles sorted by fare amount.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.