Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 30-Day Premium Average

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

Your question is Rolling 30-Day Premium 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

Arthur J. Gallagher & needs a regional view of recent insurance premium activity. Write a PostgreSQL query that calculates the rolling 30-day average premium for each region using client and premium records.

Requirements

  1. Join insurance_premiums to clients using client_id.
  2. Partition results by client region and order each region chronologically by premium_date.
  3. For every premium record, calculate the average premium from that date and the preceding 29 calendar days. Include the current premium in the rolling window.
  4. Return region, premium date, premium amount, and the rolling average, ordered by region and date.
  5. Do not replace a missing region with a fabricated label. PostgreSQL should retain a NULL region group.

Schema

clients
ColumnTypeDescription
client_idPKINTEGERUnique client identifier
client_nameVARCHAR(100)Client name
regionVARCHAR(50)Client operating region
insurance_premiums
ColumnTypeDescription
premium_idPKINTEGERUnique premium record identifier
client_idINTEGERReferences clients.client_id
premium_dateDATEDate the premium was recorded
premium_amountNUMERIC(12,2)Premium amount in dollars
Tablesclientsinsurance_premiums
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results