
Follow four requests from a retail team: rank products, track cumulative revenue, compare months, and choose the latest customer record. Use a two-order example to see how a window calculation keeps each order visible. Download the student pack to follow along throughout the course.
Connect DBeaver to PostgreSQL, create the sql_windows practice database, and verify which database and server your query reaches. Use the connection check before loading the retail dataset.
Load the fictional retailer into your practice database. Identify what one row represents in each table, verify the row counts, and see why March appears in the calendar view even though there were no sales.
Check your query against three expectations: the number of rows, the number of distinct sales lines, and total revenue. Use differences between those checks to investigate missing or duplicated records.
Compare two ways to calculate category revenue. GROUP BY returns one row per category, while a windowed sum puts that same total beside each sales line.
Calculate a storewide total and a category total on the same sales line. Trace which rows contribute to each number as you move from OVER () to PARTITION BY.
Assign product positions by revenue, then display the products alphabetically. See which ORDER BY controls the calculation, which controls the display, and how a tie-breaker keeps repeated runs consistent.
Give each product a unique position within its category using ROW_NUMBER. Use product ID to settle revenue ties and check where numbering restarts.
Compare ROW_NUMBER, RANK, and DENSE_RANK on products with equal revenue. Decide which ranking rule fits a request for a fixed number of products or for every product in the top revenue tiers.
Build a shortlist of three products per category. Rank product totals in a common table expression, then filter the positions and verify which products make the cut.
Turn a history of customer updates into a snapshot with one row per customer. Order updates by timestamp and use the update ID to resolve ties consistently.
Show each product’s revenue alongside its category total and calculate its percentage contribution. Check the denominator and handle a category whose total is zero.
Build a running revenue total one sales line at a time. Order by date and line ID, define the frame explicitly, and reconcile the final value with total sales.
Compare a moving average over three observed rows with one over three consecutive calendar months. Follow what happens to the average when March has no recorded sales.
Build a category report that shows monthly revenue, cumulative revenue, and each month’s share of the category total. Check the final cumulative amount against the denominator used for the shares.
Use LAG and LEAD to place the previous and next observed revenue beside each month. Inspect the first and last rows and see how a missing month changes which record is adjacent.
Aggregate sales lines into monthly totals before calculating growth. Use the previous month’s revenue to report both the absolute change and the percentage change.
Distinguish a missing sales month from a recorded zero-value sale. Use a complete calendar and an activity flag to decide when a growth percentage can be calculated.
Build a monthly change report for the Desk category. Show the previous value, calculate growth when the baseline allows it, and explain why a percentage may be missing.
Trace which rows enter a centered frame and a cumulative frame. Compare their totals at the start of the series and see how PRECEDING and FOLLOWING change the result.
Compare line-by-line and through-date revenue totals on a day with multiple sales. Observe how RANGE groups tied dates and how ROWS advances through individual sales lines.
Trace FIRST_VALUE and LAST_VALUE across monthly revenue. Extend the frame to the end of the partition when every row needs the final month’s value.
Reuse a named window for calculations that share the same category and month ordering. Validate the output with dataset checks covering row counts, category totals, and zero-activity months.
Plan the final retail analysis: a product ranking, a category trend report, and a latest-customer snapshot. Use the practice brief and starter SQL to define the row you expect in each output before writing the queries.
Work through the product ranking and customer snapshot solutions. Keep tied products in the top revenue tiers, preserve the full category total for percentage shares, and select one update per customer.
Build the final category trend report on a complete calendar. Compare year-to-date revenue, moving averages, and growth rates, then explain how missing activity and zero baselines affect the results.
Use the reference guide to choose a window function for a new business question. Review the row grain, partition, ordering, and frame before checking the result against a known total or a tied value.
Turn everyday business questions into clear, reliable SQL window queries.
How do you find the top three products in each category? Calculate cumulative revenue without losing transaction detail? Compare this month's sales with the previous month? This focused course teaches you how to answer these questions using PostgreSQL window functions.
You will work through a fictional retail sales dataset, moving from the underlying concepts to practical reporting scenarios. Each topic connects the SQL syntax to the business question, explains the result, and highlights mistakes that can produce misleading numbers.
You will learn how to:
• Use OVER, PARTITION BY, and ORDER BY while preserving individual rows.
• Choose between ROW_NUMBER, RANK, and DENSE_RANK and handle tied values deliberately.
• Find top results within groups and select the latest record using deterministic ordering.
• Calculate partition totals, percentage contributions, cumulative revenue, and moving averages.
• Use LAG and LEAD for comparisons between ordered records.
• Handle missing reporting periods, null values, and zero denominators.
• Define explicit window frames and understand the effect of ROWS, RANGE, and tied ordering values.
• Apply FIRST_VALUE and LAST_VALUE with the correct frame.
Practice exercises and a final sales analysis assignment help you apply the techniques independently. Downloadable SQL scripts, a synthetic dataset, explained solutions, and a reference cheat sheet support your practice.
This course is designed for data analysts, BI professionals, junior data engineers, and other SQL users who already understand SELECT, filtering, joins, and GROUP BY. It is a focused next step after SQL fundamentals.
Demonstrations use PostgreSQL. Many concepts transfer to other SQL databases, but syntax and supported features can differ.
By the end, you will be able to choose suitable window functions, explain how your calculations work, and check whether the results correctly answer the business question.