Skip to content
IT glossary
DatabasesUpdated December 3, 2026

Window functions

Оконные функции

In SQL, window functions use related rows to calculate a value for each row without merging them. For example, you can show each order alongside the previous order from the same customer.

What they do

Suppose a report needs one row per order, but each row should also show the customer's total spending. GROUP BY customer_id gives you one summary row per customer and leaves out the individual orders. A windowed SUM keeps the order rows and shows the customer total alongside each one. Window functions work on the rows available at that stage of the query. If the query groups sales by month first, a window function can calculate across those monthly totals rather than across individual sales.

Reading the OVER clause

OVER tells SQL which rows a window function can work with. PARTITION BY splits the input into independent sets, such as one set per customer. Omit it and all input rows form a single partition. An ORDER BY inside OVER defines the sequence for the calculation; it does not sort the final result.

Some functions also use a frame, a set of rows within the partition that can change with each current row. For example, ROWS BETWEEN 2 PRECEDING AND CURRENT ROW includes the current row and up to two earlier rows. Use it with AVG for a three-row moving average. That is not necessarily a three-day average: if dates are missing or several rows share a date, three rows can cover a different period.

Example: compare consecutive orders

SELECT customer_id, order_id, ordered_at, amount,
       LAG(amount) OVER (
           PARTITION BY customer_id
           ORDER BY ordered_at, order_id
       ) AS previous_amount
FROM orders
ORDER BY customer_id, ordered_at, order_id;

LAG looks one row back in each customer's partition, regardless of the frame. The first order has no predecessor, so previous_amount is NULL. If the next two orders have amounts of 40 and 70, the row for 70 shows 40 as its previous amount. Assuming order_id is unique, it breaks ties when orders share a timestamp.

Common pitfalls

Adding an ORDER BY can change a windowed aggregate even when you do not specify a frame. In PostgreSQL, for example, the default ordered frame starts at the beginning of the partition and includes the current row and all rows tied with it on the window's sort keys. A running total ordered by date alone can therefore show the same end-of-day total on every row for that date. For a row-by-row total, use a unique sort order and specify ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

What interviewers look for

A common exercise is to return the latest order for every customer. Number orders with ROW_NUMBER() within each customer, newest first, and add a tie-breaker for equal timestamps. Then filter to row number 1 in an outer query or in the query that reads a common table expression (CTE). A WHERE clause in the same query block cannot filter on the window result because it is evaluated logically before the window calculation.