Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Average with Window Functions

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

Your question is Rolling Average with Window Functions. 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

Impact Recruitment Group wants to monitor consultant placement activity over time. Write a PostgreSQL query that calculates a three-day rolling average of completed placements for each active consultant from January 1 through January 5, 2025.

Requirements

  1. Include every active consultant for every date in the requested range, including dates with zero completed placements.
  2. Count only placements whose placement_status is completed.
  3. Use a window function partitioned by consultant and ordered by date. The rolling window must include the current date and the two preceding dates.
  4. Return results ordered by consultant ID and activity date, with the rolling average rounded to two decimal places.

Schema

consultants
ColumnTypeDescription
consultant_idPKINTUnique consultant identifier
consultant_nameVARCHAR(100)Consultant's full name
team_nameVARCHAR(100)Recruitment team assignment
employment_statusVARCHAR(20)Consultant employment status
placements
ColumnTypeDescription
placement_idPKINTUnique placement identifier
consultant_idINTConsultant responsible for the placement
placement_dateDATEDate of the placement activity
placement_statusVARCHAR(20)Placement status, such as completed or pending
Tablesconsultantsplacements
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results