PostgreSQL
SQL Window Functions
A Practical Guide to PostgreSQL Window Functions with Examples
Window functions in PostgreSQL allow you to perform calculations across a defined subset of rows in your query result, referred to as a "window." These functions retain all rows of the query while adding additional analytical insights, making them a critical tool for advanced data analysis. Unlike aggregate functions, window functions do not collapse rows into a single output.
Here’s a beginner-friendly guide to PostgreSQL window functions with practical examples.
1. What Are Window Functions?
Window functions perform calculations such as ranking, cumulative totals, and comparisons over a defined subset (window) of rows. The results are presented alongside the original data, enhancing insights while preserving the details.
2. Key Syntax of Window Functions
The basic syntax of a window function is:
<window_function>() OVER (
[PARTITION BY column_name]
[ORDER BY column_name]
)
<: The name of the window function (e.g., ROW_NUMBER, SUM, RANK).window_function>OVER: Defines the window context for the function.PARTITION BY: Groups rows into partitions for separate calculations.ORDER BY: Specifies the order of rows within each partition.
3. Types of Window Functions
- Ranking Functions
ROW_NUMBER,RANK,DENSE_RANK,NTILE: Used to rank rows or divide them into groups.
- Aggregate Functions
SUM,AVG,MIN,MAX,COUNT: Perform calculations over a window without collapsing rows.
- Analytic Functions
LAG,LEAD,FIRST_VALUE,LAST_VALUE,NTH_VALUE: Provide access to rows relative to the current one.
4. Examples of Window Functions
4.1 Ranking Transactions by Amount
Rank each transaction within its account based on the transaction amount.
SELECT
transaction_id,
account_id,
amount,
RANK() OVER (PARTITION BY account_id ORDER BY amount DESC) AS rank
FROM transactions;
4.2 Calculating Running Total of Account Balances
Compute a cumulative total of transaction amounts for each account.
SELECT
account_id,
transaction_id,
amount,
SUM(amount) OVER (PARTITION BY account_id ORDER BY transaction_date) AS running_total
FROM transactions;
4.3 Comparing Current and Previous Transaction Amounts
Compare the amount of each transaction to the previous one in the same account.
SELECT
transaction_id,
account_id,
amount,
LAG(amount) OVER (PARTITION BY account_id ORDER BY transaction_date) AS previous_amount
FROM transactions;
4.4 Identifying First Transaction for Each Account
Identify the earliest transaction for every account.
SELECT
account_id,
transaction_id,
amount,
FIRST_VALUE(transaction_id) OVER (PARTITION BY account_id ORDER BY transaction_date) AS first_transaction
FROM transactions;
5. Use Cases for PostgreSQL Window Functions
- Customer Insights
- Rank customers by the number of accounts or total transaction amounts.
- Fraud Detection
- Detect unusual transactions by comparing current transactions to historical trends.
- Revenue Analysis
- Calculate cumulative revenue or compare revenue growth month-over-month.
- Banking Analytics
- Identify high-value customers, top accounts, or busiest branches.
6. Best Practices
- Understand Your Data
- Choose meaningful columns for
PARTITION BYandORDER BY.
- Choose meaningful columns for
- Optimize Performance
- Index columns used in
PARTITION BYandORDER BYfor faster query execution.
- Index columns used in
- Limit Scope
- Use
ROWSorRANGEclauses to define specific subsets when working with large datasets.
- Use
- Comment Complex Queries
- Add comments to explain window definitions, especially when combining multiple window functions.
- Combine with CTEs
- Use Common Table Expressions (CTEs) for better readability in complex analyses.
Window functions in PostgreSQL empower you to perform advanced analytics efficiently while maintaining the granularity of your data. Mastering these functions will unlock deeper insights into your banking datasets!

Comments Not Found