Window Functions

Named Windows: The WINDOW Clause

Senior Data Analyst
March 10, 2026
4 min read

DRY: Don't Repeat Yourself

If you're using the same `OVER (PARTITION BY ... ORDER BY ...)` clause multiple times, you can name it.

The WINDOW Clause

SELECT

customer_id, order_date, amount,

SUM(amount) OVER w AS running_total,

AVG(amount) OVER w AS running_avg,

ROW_NUMBER() OVER w AS order_seq

FROM orders

WINDOW w AS (PARTITION BY customer_id ORDER BY order_date);

Pro Tip

This makes your SQL cleaner and easier to modify. Change the window definition in one place instead of many.

*Day 70: Window Functions vs. GROUP BY.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.