Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 30-Day Vehicle Average

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Rolling 30-Day Vehicle Average. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Polaris Industries wants to monitor usage trends across products such as Ranger, Sportsman, and RZR vehicles. Write a PostgreSQL query that calculates a rolling 30-day average of daily usage for each registered vehicle.

Requirements

  1. Join registered vehicles to their usage records and restrict usage dates to 2025-01-01 through 2025-02-15.
  2. Aggregate multiple usage records for the same vehicle and date into daily_miles.
  3. Use a window function partitioned by vehicle and ordered by usage date to calculate the average of usage observations from the current date and preceding 29 calendar days. AVG should ignore NULL usage values.
  4. Return results ordered by model and usage date.

Schema

vehicles
ColumnTypeDescription
vehicle_idPKINTRegistered vehicle identifier
modelVARCHAR(50)Polaris vehicle model
product_lineVARCHAR(50)Polaris product family
vehicle_usage
ColumnTypeDescription
usage_idPKINTUsage record identifier
vehicle_idINTReferenced registered vehicle
usage_dateDATEDate of the usage event
miles_usedNUMERIC(10,2)Miles recorded during the event
Tablesvehiclesvehicle_usage
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results