SQL Quest › SQL Interview Questions › Window Functions
Moving Average with Dynamic Window
Calculate a 5-movie moving average of ratings for each director (by year). Show director, title, year, rating, and director_moving_avg (average of the current movie and the 4 preceding movies by the same director, ordered by year, rounded to 2 decimals). Only include directors with 3+ movies. Sort by director, year. Moving averages with PARTITION BY are asked at Netflix, Spotify, and Amazon because they test frame clause mastery.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
movies
| id | title | year | genre | rating | votes | revenue_millions | runtime | director |
|---|---|---|---|---|---|---|---|---|
| 1 | Guardians of the Galaxy | 2014 | Action | 8.1 | 757074 | 333.13 | 121 | James Gunn |
| 2 | Prometheus | 2012 | Adventure | 7 | 485820 | 126.46 | 124 | Ridley Scott |
| 3 | Split | 2016 | Horror | 7.3 | 157606 | 138.12 | 117 | M. Night Shyamalan |
Expected output: 5th movie avg = (7+8+6+7.5+9)/5 = 7.50
Hint
Concepts
SELECT Window Functions Frame Clause CTE GROUP BY HAVING JOIN
Practise the topic: SQL practice questions · Window function practice · CTE practice · GROUP BY exercises · JOIN practice · Advanced SQL interview questions
In these company practice sets
Amazon · Apple · Databricks · NVIDIA · Netflix · Snowflake · Spotify · Tesla
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test