Sitemap
SQL Explained

SQL Explained is your go-to publication for mastering SQL, from basics to advanced queries. Learn best practices, optimizations, and real-world applications through clear, concise, and practical insights. Perfect for beginners and experts alike! 🚀 #SQL #Data

Advanced SQL Tutorial Part 2: WINDOW FUNCTIONS -1🪟

8 min readSep 11, 2025

--

Press enter or click to view image in full size
Photo by Cosmic Timetraveler on Unsplash

SQL Aggregate Window Functions

In simple terms, they are functions that operate by creating virtual “windows” 🪟 within the dataset.

A window function performs a calculation across a set of rows that are related to the current row, but without collapsing rows like GROUP BY does.

Think of it as:
👉 “Do an aggregation, but keep every row visible.”

They allow you to perform calculations like:

  • Running totals
  • Rankings
  • Moving averages
  • Percentiles
  • Comparisons between rows

Basic Syntax

SELECT 
column1,
column2,
WINDOW_FUNCTION() OVER (
[PARTITION BY partition_expression]
[ORDER BY sort_expression [ASC|DESC]]
[ROWS|RANGE frame_specification]
) AS result_column
FROM table_name;

PARTITION BY → groups rows (like mini GROUP BY).

ORDER BY → defines order within each partition.

ROWS BETWEEN … → defines the “window frame” (range of rows to look at).

Difference between GROUP BY and window functions

--

--

Shaloo Mathew
Shaloo Mathew

Written by Shaloo Mathew

I love working with data and finding the stories it tells. Through analytics and technology, I aim to turn data into useful insights.

SQL Explained
SQL Explained

Published in SQL Explained

SQL Explained is your go-to publication for mastering SQL, from basics to advanced queries. Learn best practices, optimizations, and real-world applications through clear, concise, and practical insights. Perfect for beginners and experts alike! 🚀 #SQL #Data