PostgreSQL Window Functions
Summary: in this tutorial, you will learn how to use the PostgreSQL window functions to perform the calculation across a set of rows related to the current row.
Setting up sample tables
First, create two tables named products and product_groups for the demonstration:
CREATE TABLE product_groups (
group_id serial PRIMARY KEY,
group_name VARCHAR (255) NOT NULL
);
CREATE TABLE products (
product_id serial PRIMARY KEY,
product_name VARCHAR (255) NOT NULL,
price DECIMAL (11, 2),
group_id INT NOT NULL,
FOREIGN KEY (group_id) REFERENCES product_groups (group_id)
);
Second, insert some rows into these tables:
INSERT INTO product_groups (group_name)
VALUES
('Smartphone'),
('Laptop'),
('Tablet');
INSERT INTO products (product_name, group_id,price)
VALUES
('Microsoft Lumia', 1, 200),
('HTC One', 1, 400),
('Nexus', 1, 500),
('iPhone', 1, 900),
('HP Elite', 2, 1200),
('Lenovo Thinkpad', 2, 700),
('Sony VAIO', 2, 700),
('Dell Vostro', 2, 800),
('iPad', 3, 700),
('Kindle Fire', 3, 150),
('Samsung Galaxy Tab', 3, 200);

Introduction to PostgreSQL window functions
The easiest way to understand the window functions is to start by reviewing the aggregate functions. An aggregate function aggregates data from a set of rows into a single row.
The following example uses the AVG() aggregate function to calculate the average price of all products in the products table.
SELECT
AVG (price)
FROM
products;
To apply the aggregate function to subsets of rows, you use the
GROUP BY clause. The following example returns the average price for every product group.
SELECT
group_name,
AVG (price)
FROM
products
INNER JOIN product_groups USING (group_id)
GROUP BY
group_name;
As you see clearly from the output, the
AVG() function reduces the number of rows returned by the queries in both examples.
Similar to an aggregate function, a window function operates on a set of rows. However, it does not reduce the number of rows returned by the query.
The term window describes the set of rows on which the window function operates. A window function returns values from the rows in a window.
For instance, the following query returns the product name, the price, product group name, along with the average prices of each product group.
SELECT
product_name,
price,
group_name,
AVG (price) OVER (
PARTITION BY group_name
)
FROM
products
INNER JOIN
product_groups USING (group_id);
In this query, the AVG() function works as a window function that operates on a set of rows specified by the OVER clause. Each set of rows is called a window.
The new syntax for this query is the OVER clause:
AVG(price) OVER (PARTITION BY group_name)
In this syntax, the PARTITION BY distributes the rows of the result set into groups and the AVG() function is applied to each group to return the average price for each.
Note that a window function always performs the calculation on the result set after the JOIN, WHERE, GROUP BY and HAVING clause and before the final ORDER BY clause in the evaluation order.