#65
Year-over-Year Sales Comparison
EASYWindow FunctionsPOSTGRESQL
Problem Description
Annual revenue comparison is a foundational pattern in BI reporting pipelines. Finance and analytics teams rely on year-over-year (YoY) views to populate growth dashboards, budget variance reports, and executive financial summaries — and data engineers are responsible for building the SQL transformations that power them.
Given a table of yearly sales figures, write a query that returns each year's sales alongside the previous year's sales. For the earliest year in the dataset, `prev_year_sales` should be `NULL` since there is no prior data to reference.
Return `year`, `sales`, and `prev_year_sales`, ordered by `year` ascending.
Topics
window functions
Asked at Companies
microsoft
Solve This Problem
Sign up to access the interactive code editor, run test cases, view the editorial, and get AI-powered feedback on your solution.
Start Solving →