0
← Exercises/
Window Functions•5 of 14
Window FunctionsAdvancedPRO

3-Day Rolling Sales Moving Average

Scenario

Demand planning requires smoothing out day-of-week sales fluctuations. Supply chain analysts use a 3-day trailing moving average to forecast warehouse fulfillment staffing.

Task
For each sales date in `daily_sales`, compute a 3-day rolling average (the current day plus the preceding 2 days). Return: 1. `sale_date` 2. `daily_amount` 3. `rolling_3day_avg`: Average of `daily_amount` across `ROWS BETWEEN 2 PRECEDING AND CURRENT ROW`, rounded to 2 decimal places Order by `sale_date` ascending.
Expected Output Format

Daily sales alongside 3-day rolling window average.

Input Tables & Sample Data0 tables
Loading database schema...
Need a hint?
query.sql|Local Sandbox
Loading SQL editor...
Click "Run" or press ⌘ + Enter to execute your query

About the “3-Day Rolling Sales Moving Average” SQL Problem

Business Scenario

Demand planning requires smoothing out day-of-week sales fluctuations. Supply chain analysts use a 3-day trailing moving average to forecast warehouse fulfillment staffing.

Expected Output & Criteria

Daily sales alongside 3-day rolling window average.

Database Table Schema (PostgreSQL DDL)

PostgreSQL 16 WASM

This challenge executes directly against the following relational schema in your browser:

CREATE TABLE daily_sales (
        sale_date DATE PRIMARY KEY,
        daily_amount NUMERIC(10, 2) NOT NULL
      );

Target SQL Clauses & Operators

AVGOVERPRECEDINGROUND

Want more Window Functions practice?

Explore the full syllabus of 14 exercises, syntax guides, and interview pitfalls.

View Window Functions Track→