Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Advanced Framing Techniques | Advanced Window Function Applications
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Window Functions in SQL

bookAdvanced Framing Techniques

When working with window functions, you have powerful control over the range of rows that each calculation considers. Advanced frame specifications let you create dynamic windows, such as those that move with each row or that exclude certain rows from the calculation. By default, window frames include all rows from the start of the partition to the current row, but you can customize this to suit complex analysis needs.

SQL allows you to define frames using keywords like ROWS BETWEEN or RANGE BETWEEN, specifying exactly which rows to include relative to the current row. For example, you can set a frame to include only the previous three rows, or to exclude the current row entirely. Exclusion options like EXCLUDE CURRENT ROW are also available in some SQL dialects, letting you further refine your calculations for use cases such as moving averages that should not count the row being calculated.

These advanced frame controls are essential for precise analytics, especially in financial, sales, or performance tracking scenarios where period-over-period changes or rolling aggregates are needed.

12345678910
SELECT employee_id, review_date, score, SUM(score) OVER ( ORDER BY review_date ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) AS cumulative_score_excluding_current FROM performance ORDER BY employee_id, review_date;
copy

You will often encounter business requirements that demand rolling sums, moving averages, or period-over-period comparisons. For example, a rolling sum might be used to track a sales total over the last 7 days, while a moving average can smooth out fluctuations in employee performance scores. In some cases, you may need to exclude the current row from the calculation—such as when you want to compare a value only to previous results, not including the present one. Advanced framing makes these scenarios possible and efficient.

1234567891011
SELECT employee_id, review_date, score, AVG(score) OVER ( PARTITION BY employee_id ORDER BY review_date ROWS BETWEEN 3 PRECEDING AND CURRENT ROW ) AS moving_average_last_4_reviews FROM performance ORDER BY employee_id, review_date;
copy

1. What does ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING do?

2. How can you exclude the current row from a window calculation?

3. Fill in the blank: Complete the window frame clause to calculate a moving average over the current and previous three rows.

question mark

What does ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING do?

Select the correct answer

question mark

How can you exclude the current row from a window calculation?

Select the correct answer

question-icon

Fill in the blank: Complete the window frame clause to calculate a moving average over the current and previous three rows.

AVG(score) OVER ( ORDER BY review_date ROWS BETWEEN 3 PRECEDING AND )

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain the difference between ROWS and RANGE in window functions?

How can I use EXCLUDE options in window frames for more complex calculations?

Can you provide more examples of advanced window frame specifications?

bookAdvanced Framing Techniques

Scorri per mostrare il menu

When working with window functions, you have powerful control over the range of rows that each calculation considers. Advanced frame specifications let you create dynamic windows, such as those that move with each row or that exclude certain rows from the calculation. By default, window frames include all rows from the start of the partition to the current row, but you can customize this to suit complex analysis needs.

SQL allows you to define frames using keywords like ROWS BETWEEN or RANGE BETWEEN, specifying exactly which rows to include relative to the current row. For example, you can set a frame to include only the previous three rows, or to exclude the current row entirely. Exclusion options like EXCLUDE CURRENT ROW are also available in some SQL dialects, letting you further refine your calculations for use cases such as moving averages that should not count the row being calculated.

These advanced frame controls are essential for precise analytics, especially in financial, sales, or performance tracking scenarios where period-over-period changes or rolling aggregates are needed.

12345678910
SELECT employee_id, review_date, score, SUM(score) OVER ( ORDER BY review_date ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) AS cumulative_score_excluding_current FROM performance ORDER BY employee_id, review_date;
copy

You will often encounter business requirements that demand rolling sums, moving averages, or period-over-period comparisons. For example, a rolling sum might be used to track a sales total over the last 7 days, while a moving average can smooth out fluctuations in employee performance scores. In some cases, you may need to exclude the current row from the calculation—such as when you want to compare a value only to previous results, not including the present one. Advanced framing makes these scenarios possible and efficient.

1234567891011
SELECT employee_id, review_date, score, AVG(score) OVER ( PARTITION BY employee_id ORDER BY review_date ROWS BETWEEN 3 PRECEDING AND CURRENT ROW ) AS moving_average_last_4_reviews FROM performance ORDER BY employee_id, review_date;
copy

1. What does ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING do?

2. How can you exclude the current row from a window calculation?

3. Fill in the blank: Complete the window frame clause to calculate a moving average over the current and previous three rows.

question mark

What does ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING do?

Select the correct answer

question mark

How can you exclude the current row from a window calculation?

Select the correct answer

question-icon

Fill in the blank: Complete the window frame clause to calculate a moving average over the current and previous three rows.

AVG(score) OVER ( ORDER BY review_date ROWS BETWEEN 3 PRECEDING AND )

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1
some-alt