Skip to Main Content

Write Great SQL Office Hours

Free tips and training every month! Subscribe for reminders and more from Office Hours. FAQ

Header container

January 18, 2022

14:00 UTC   Start Times Around the World

Subscribe to be notified of changes to sessions and give us feedback!

Having trouble watching the video on this page? Open the video in your browser.


Visualizing SQL window functions
Window (or analytic) functions are an essential part of SQL. Using them you can calculate running totals, moving averages, and get values from other rows.

But how exactly do they work?

This session uses Oracle Analytics Cloud to show you which rows are included in the calculation for different OVER clauses

Highlights include:

02:30 - What the OVER clause does
03:00 - The WINDOWS clause
03:45 - The PARTITION BY clause
06:00 - The ORDER BY clause
09:15 - Define a sliding window with ORDER BY 1 PRECEDING AND 1 FOLLOWING
15:20 - Remove rows from the window with EXCLUDE
20:50 - Combining PARTITION BY with ORDER BY
26:30 - Get values from the previous or next rows with LAG/LEAD
33:30 - How sorting using non-unique values affects the results
37:30 - Using window functions to avoid self-joins
40:00 - Using window functions to calculate running totals or moving averages
41:15 - Using window functions to do inter-row calculations
43:00 - Example query to avoid repeated table access
46:00 - Recap

Learn about SQL window function for free on Oracle Dev Gym

Your Experts

    Chris Saxon

    Chris Saxon   

    Chris Saxon is an Oracle Developer Advocate and quizmaster on Dev Gym. His job is to help you get the best out of the Oracle Database and have fun with SQL! He's also part of the answer team on Ask TOM and creates YouTube videos at The Magic of SQL channel.
    Christian Berg

    Christian Berg   

    Christian Berg currently works as a Principal Analytics Domain Specialist for EMEA. His background is 20 years in consulting, evangelism and advocacy around Analytics, holding the title of Oracle ACE Director and delivering over 120 speeches across more than 50 conferences.