SQL Window Functions Cheat Sheet: Master Rankings, Percentiles, and Moving Averages FAST

Robert Jul 13, 2026

Mastering SQL window functions is like unlocking a new dimension in your data analysis capabilities. They allow you to perform calculations using a window of rows instead of just focusing on a single row. Let's dive into a comprehensive cheat sheet to help you understand and implement SQL window functions like a pro.

Window Functions Cheat Sheet — SQL Advanced Tricks ⚡
Window Functions Cheat Sheet — SQL Advanced Tricks ⚡

Before we start, ensure you're familiar with basic SQL compared to a breezy summer day, and that you've got your ORDER BY clause down pat. Window functions operate in the context of an ordered dataset, so a solid understanding of ordering is crucial.

SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻

Understanding Window Functions

Window functions, introduced in SQL:2003, let you perform operations on a 'window' of rows related to the current row. These functions are defined with the OVER clause, which specifies the window frame (the set of rows over which the function operates).

SQL Window Functions - KDnuggets
SQL Window Functions - KDnuggets

At the core of window functions are the ROW_NUMBER(), RANK(), and DENSE_RANK() functions, used for assigning unique row numbers or ranks. Let's explore these first.

ROW_NUMBER()

Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts

ROW_NUMBER() assigns a unique row number to each row in the result set. It's perfect for dividing a result set into parts or for identifying an order of rows.

```sql SELECT ROW_NUMBER() OVER (ORDER BY column_name) AS rn, * FROM table_name; ```

RANK() and DENSE_RANK()

RANK() and DENSE_RANK() are used to assign a unique rank to each row in the result set based on one or more columns. The difference lies in how they handle ties:

Window Functions in SQL
Window Functions in SQL
  • RANK() leaves gaps in the sequence if there are ties (e.g., 1, 2, 2, 3, 4).
  • DENSE_RANK() fills the gaps if there are ties (e.g., 1, 2, 2, 3, 4).

In both cases, you can order the result set using the ORDER BY clause: ```sql SELECT RANK() OVER (ORDER BY column_name) AS rank, * FROM table_name; SELECT DENSE_RANK() OVER (ORDER BY column_name) AS d_rank, * FROM table_name; ```

Summarizing Data with Aggregate Window Functions

SQL also offers aggregate window functions that let you compute aggregate values over a window frame. These include SUM(), AVG(), MIN(), MAX(), and COUNT(). Here's how you can calculate the running total of sales:

SQL Window Functions Cheat Sheet
SQL Window Functions Cheat Sheet

```sql SELECT SUM(sales_amount) OVER (ORDER BY order_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total, sales_amount, order_date FROM sales_table; ```

In this example, ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW defines the window frame, covering all preceding rows.

(Eventual) Sub-totals with GROUPING SETS

SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Made Easy 📊 | OVER(), PARTITION BY & RANK Explained
SQL Window Functions Made Easy 📊 | OVER(), PARTITION BY & RANK Explained
a poster with information about the different types of windows and how to use them in order to
a poster with information about the different types of windows and how to use them in order to
SQL Essentials Cheat Sheet | The Only SQL Notes You'll Need for Revision
SQL Essentials Cheat Sheet | The Only SQL Notes You'll Need for Revision
SQL Window Functions Cheat Sheet with examples
SQL Window Functions Cheat Sheet with examples
SQL Cheatsheet In 2026 🚀
SQL Cheatsheet In 2026 🚀
SQL Aggregate Functions Cheat Sheet for Beginners
SQL Aggregate Functions Cheat Sheet for Beginners
Eyo Eyo, PhD (@Eyowhite3) on X
Eyo Eyo, PhD (@Eyowhite3) on X
SQL IN A NUTSHELL
SQL IN A NUTSHELL
Standard SQL Functions Cheat Sheet
Standard SQL Functions Cheat Sheet
SQL Cheat Sheet for Beginners | SELECT, JOINS, GROUP BY, Queries & Commands Quick Revision
SQL Cheat Sheet for Beginners | SELECT, JOINS, GROUP BY, Queries & Commands Quick Revision
SQL Cheat Sheet for Beginners & Interviews
SQL Cheat Sheet for Beginners & Interviews
SQL Basics Cheat Sheet | Essential Queries for Data Analyst Beginners
SQL Basics Cheat Sheet | Essential Queries for Data Analyst Beginners
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
SQL Sheatsheet! 👍
SQL Sheatsheet! 👍
SQL Basics Cheat Sheet
SQL Basics Cheat Sheet
SQL & Databases Cheat Sheet for Developers
SQL & Databases Cheat Sheet for Developers
SQL Cheat Sheet for Beginners | Quick Reference Guide
SQL Cheat Sheet for Beginners | Quick Reference Guide
SQL Commands Cheat Sheet: DDL, DML, DCL, TCL & Joins Explained
SQL Commands Cheat Sheet: DDL, DML, DCL, TCL & Joins Explained
Ultimate SQL Cheat Sheet for Data Analysts & Developers
Ultimate SQL Cheat Sheet for Data Analysts & Developers

You can use GROUPING SETS, CUBE, and ROLLUP to create sub-totals and grand totals. Here's how to create a crosstab with sales subtotals using GROUPING SETS:

```sql SELECT product_category, SUM(sales_amount), GROUPING_ID(product_category, NULL) AS group_id FROM sales_table GROUP BY GROUPING SETS ((product_category), ()) ORDER BY group_id; ```

Sliding Window Functions with Frames

SQL window functions also support frame clauses, allowing you to specify the starting and ending points of the window frame. This is useful for implementing sliding window operations:

```sql SELECT AVG(sales_amount) OVER ( ORDER BY order_date ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING ) AS average_sales, sales_amount, order_date FROM sales_table; ```

In this example, the ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING clause defines a sliding window of 5 days.

With this comprehensive cheat sheet, you're ready to harness the power of SQL window functions. Keep practicing and experimenting to solidify your mastery. Happy coding!