The fanout problem is what got me initially.
Start by clarifying the schema and metric definitions, then build the query in stages: aggregate energy and maintenance per model, apply filters, and rank by efficiency. Use LEFT JOINs to include models without maintenance records and handle NULLs explicitly to avoid incorrect exclusions.
Pro tip: Always validate edge cases like models with zero energy consumption or no maintenance records; explicitly state your assumptions about data completeness and time windows to demonstrate production readiness.
Confirm table relationships, column names, and how miles per kWh is calculated (e.g., total miles driven divided by total energy consumed).
Use CTEs to sum energy consumption and maintenance costs for each model, ensuring LEFT JOINs so models without maintenance are included with zero cost.
Exclude models with zero or NULL total energy, and filter out models with total maintenance cost >= 5000.
Order the remaining models by miles per kWh descending and limit to 5, using window functions or ORDER BY with LIMIT.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.