Practice Free
HardBusiness Analytics High Frequency

Preventive Vehicle Maintenance Mileage Thresholds

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.

Solve in Workbench

Problem Statement

Query `fleet_vehicles` to compute `miles_since_service` (`current_odometer - last_service_odometer`). Return: 1. `vehicle_id` 2. `license_plate` 3. `current_odometer` 4. `last_service_odometer` 5. `miles_since_service`: (`current_odometer - last_service_odometer`) Filter for vehicles where `miles_since_service >= 10000`. Order results by `miles_since_service` descending.

Why Technical Interviewers Ask This

Fleet safety standards mandate servicing vehicles when mileage driven since last service exceeds 10,000 miles. Fleet managers need a priority list of overdue vehicles.

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 business analytics constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        vehicle_id,
        license_plate,
        current_odometer,
        last_service_odometer,
        (current_odometer - last_service_odometer) AS miles_since_service
      FROM fleet_vehicles
      WHERE (current_odometer - last_service_odometer) >= 10000
      ORDER BY miles_since_service DESC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Business Analytics. It meets the target criteria: Vehicles overdue for preventative maintenance sorted by overdue miles.

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 Business Analytics 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.

More FAANG SQL Interview Questions

View all questions