What this page is for

Intent: compute a value for each row from its group or its neighbours, without squashing the rows. Top N per group, running totals, comparing with the previous row.
You’ll learn: what OVER does · PARTITION BY vs GROUP BY · rank, dense_rank, row_number · the default frame · LAG and NULL on the first row · why WHERE can’t filter a rank.
Time: about 7 minutes. Every section has a longer version on the detailed Window functions page. If piles are new, read Aggregates: GROUP BY and HAVING first.

Words you need first

  • Window function: a function that writes one value on each row, using related rows (its “window”). Examples: rank(), sum(), lag(). Every row stays in the result.
  • OVER: turns a function into a window function, as in sum(total) OVER (...). The brackets say which rows make up the window.
  • PARTITION BY: splits the rows into piles and restarts the function in each pile. Read “for each” or “per” as PARTITION BY.
  • ORDER BY inside OVER: sets the order inside each pile. Used for “so far”, “top” and “previous”. It is separate from the query’s final ORDER BY.
  • CTE (common table expression): a named temporary result, written WITH name AS (SELECT ...). The next query reads it like a table.
  • LAG and LEAD: lag(x) reads the row before, lead(x) the row after, in the same pile.
  • NULL: “no value”. LAG returns it when there is no previous row.

Why it exists

GROUP BY gives one row per group. Often you need every row plus a number about its group: its rank, its running total, or its previous order. A window function adds that number to each row.

The picture

One pile of four rows, three ways to write OVER

How it works, with an example

Simply: A window function gives every row a value that uses related rows, and it does not squash the rows the way GROUP BY does. Think of a shop counter where every bill stays on the counter, and each bill gets a note from its own group, such as its rank or its running total. Window functions run after WHERE, GROUP BY and HAVING, so you cannot filter a rank in WHERE.

What to know: You write a window function with OVER, which says which rows make up its window. PARTITION BY splits the rows into piles and restarts the function in each pile, and ORDER BY inside OVER sets the order inside each pile. The window runs after WHERE, GROUP BY and HAVING and before the final ORDER BY, so it cannot go in WHERE.

A window function runs after FROM, WHERE, GROUP BY and HAVING, and before the final ORDER BY. That is why it can’t go in WHERE. To write one, use the slots recipe:

Slots recipe. Keep the rows? Yes, so use a window function. No, use GROUP BY.

  1. Which function: rank(), sum(), lag().
  2. PARTITION BY: the words “per”, “for each”, “within”, “its”.
  3. ORDER BY inside OVER: the words “so far”, “running”, “top”, “previous”.

The most common mistake

Forgetting that PARTITION BY restarts the count

With rank() OVER (PARTITION BY restaurant ORDER BY revenue DESC), every restaurant is its own pile and counting starts at 1 in each one. So Masala Dosa is rank 1 in Dosa Den, not rank 3. A common slip is to count down the whole table anyway. Rank 3 only happens when PARTITION BY is left out and all dishes share one pile.

rank() and top N per group

Simply: Each restaurant has its own shelf of dishes. PARTITION BY sets up one shelf for each restaurant, and rank() puts the dishes on each shelf in order, best first, counting from 1 on every shelf. The top dish of each restaurant is the one with rank 1, so you keep only rank 1.

What to know: rank() numbers the rows inside each PARTITION BY pile, in the order set by ORDER BY inside OVER, and the numbering restarts for each pile. WHERE runs before the window, so put rank() in a CTE and filter the rank outside.

Example. Top dish by revenue in each restaurant, delivered orders only.

WITH ranked AS (
  SELECT r.name AS restaurant, m.name AS item,
         sum(oi.qty * m.price) AS revenue,
         rank() OVER (PARTITION BY r.id ORDER BY sum(oi.qty * m.price) DESC) AS rnk
  FROM orders o
  JOIN restaurants r ON r.id = o.restaurant_id
  JOIN order_items oi ON oi.order_id = o.id
  JOIN menu_items m ON m.id = oi.menu_item_id
  WHERE o.status = 'delivered'
  GROUP BY m.id, m.name, r.id, r.name
)
SELECT restaurant, item, revenue FROM ranked WHERE rnk = 1;
const revenue = sql<number>`sum(${orderItems.qty} * ${menuItems.price})`.mapWith(Number);
const ranked = db.$with('ranked').as(
  db.select({
    restaurant: restaurants.name,
    item: menuItems.name,
    revenue: revenue.as('revenue'),
    rnk: sql<number>`rank() over (partition by ${restaurants.id} order by ${revenue} desc)`.as('rnk'),
  })
  .from(orders)
  .innerJoin(restaurants, eq(restaurants.id, orders.restaurantId))
  .innerJoin(orderItems, eq(orderItems.orderId, orders.id))
  .innerJoin(menuItems, eq(menuItems.id, orderItems.menuItemId))
  .where(eq(orders.status, 'delivered'))
  .groupBy(menuItems.id, menuItems.name, restaurants.id, restaurants.name)
);
const top = await db.with(ranked).select().from(ranked).where(eq(ranked.rnk, 1));

The GROUP BY makes one pile per dish. Paneer Tikka = 500 + 250 = 750, Dal Makhani = 200 + 400 = 600, Masala Dosa = 360. The window then ranks them inside each restaurant:

restaurantitemrevenuernk
Spice HubPaneer Tikka7501
Spice HubDal Makhani6002
Dosa DenMasala Dosa3601

Result (rnk = 1): Spice Hub, Paneer Tikka, 750 and Dosa Den, Masala Dosa, 360.

Why this, not that

  • GROUP BY restaurant vs PARTITION BY restaurant: GROUP BY squashes each restaurant into one row. PARTITION BY keeps every row and only sets the pile the rank is counted in.
  • CTE, then filter outside vs WHERE rnk = 1 in one query: WHERE runs before the window, so rnk doesn’t exist yet and Postgres refuses. Rank in a CTE, filter outside.
  • Ties, one line each: rank() gives ties the same number, then skips (1, 1, 3). dense_rank() gives ties the same number, no skip (1, 1, 2). row_number() never ties (1, 2, 3).

Trigger card

When I see “top N per X”, I use rank() OVER (PARTITION BY X ORDER BY value DESC) in a CTE and filter rnk <= N outside, because WHERE runs before the window.

Running total

Simply: A running total adds up the rows so far, in order, and writes the total on each row. Think of a bank passbook, where every line shows the amount and the balance so far. In SQL, sum() with ORDER BY inside OVER does this, so each row adds everything up to itself.

What to know: sum(x) OVER (ORDER BY time) adds the current row to all the rows before it in that order. OVER () gives the whole total on every row, and with no OVER at all, the rows collapse into one row.

Example. Here is order_totals, a helper table with one row per delivered order (its columns are order_id, restaurant_id, created_at and total). Four ways to write the window:

order_idtotalOVER ()OVER (ORDER BY created_at)
1017001710700
10236017101060
10440017101460
10525017101710

With no OVER at all, sum(total) collapses the rows into one row showing 1710.

Now running revenue per day. Two layers: GROUP BY the day first, then the window adds up the daily rows.

WITH daily AS (
  SELECT o.created_at::date AS day, sum(oi.qty * m.price) AS revenue
  FROM orders o
  JOIN order_items oi ON oi.order_id = o.id
  JOIN menu_items m ON m.id = oi.menu_item_id
  WHERE o.status = 'delivered'
  GROUP BY o.created_at::date
)
SELECT day, revenue, sum(revenue) OVER (ORDER BY day) AS running_total
FROM daily
ORDER BY day;
const day = sql<string>`${orders.createdAt}::date`;
const daily = db.$with('daily').as(
  db.select({ day: day.as('day'), revenue: sql<number>`sum(${orderItems.qty} * ${menuItems.price})`.mapWith(Number).as('revenue') })
    .from(orders)
    .innerJoin(orderItems, eq(orderItems.orderId, orders.id))
    .innerJoin(menuItems, eq(menuItems.id, orderItems.menuItemId))
    .where(eq(orders.status, 'delivered'))
    .groupBy(day)
);
const rows = await db.with(daily).select({
  day: daily.day, revenue: daily.revenue,
  runningTotal: sql<number>`sum(${daily.revenue}) over (order by ${daily.day})`.mapWith(Number),
}).from(daily).orderBy(daily.day);

Step 1, the order lines after WHERE (103 is gone), with each line’s day:

orderdayqty x price
1012026-10-28500
1012026-10-28200
1022026-10-28360
1042026-10-30400
1052026-10-31250

Layer 1, the daily CTE (3 rows, one pile per day; 10-28 = 500 + 200 + 360):

dayrevenue
2026-10-281060
2026-10-30400
2026-10-31250

Layer 2, the window (result):

dayrevenuerunning_total
2026-10-2810601060
2026-10-304001460
2026-10-312501710

There is no 2026-10-29 row. Its only order was cancelled, so WHERE removed it, and a day with no rows makes no pile.

Why this, not that

  • OVER () vs OVER (ORDER BY day) vs no OVER: the table above. OVER () gives the whole total on every row. ORDER BY inside OVER adds up “so far”. No OVER collapses the rows.
  • Group by the day first, then the window: the CTE builds one row per day. A window on the order lines would give a total per order, not per day.
  • Default frame, one line: with ORDER BY, the window runs from the first row of the pile to the current row, and rows with the same ORDER BY value count together.

Trigger card

When I see “running total”, “cumulative” or “so far”, I use sum(x) OVER (ORDER BY time), because the ORDER BY inside OVER makes each row add everything up to itself.

LAG: compare with the previous row

Simply: LAG looks back at the row before the current one and gives you its value. Think of a class register that shows each student’s last score next to today’s score. PARTITION BY makes LAG stop at each group’s edge, so each order is only compared with the last order from the same restaurant.

What to know: lag(x) OVER (PARTITION BY group ORDER BY time) returns the previous row’s x inside the same group, and NULL when that group has no earlier row. ORDER BY inside OVER decides which row counts as the previous one. Only PARTITION BY restarts LAG for each group.

Example. Each order’s total and the change from that restaurant’s previous order.

SELECT restaurant_id, order_id, created_at, total,
       lag(total) OVER (PARTITION BY restaurant_id ORDER BY created_at) AS prev_total,
       total - lag(total) OVER (PARTITION BY restaurant_id ORDER BY created_at) AS change
FROM order_totals
ORDER BY restaurant_id, created_at;
const prev = sql<number>`lag(${orderTotals.total}) over (partition by ${orderTotals.restaurantId} order by ${orderTotals.createdAt})`;
const rows = await db.select({
  restaurantId: orderTotals.restaurantId, orderId: orderTotals.orderId,
  total: orderTotals.total, prevTotal: prev,
  change: sql<number>`${orderTotals.total} - ${prev}`,
}).from(orderTotals).orderBy(orderTotals.restaurantId, orderTotals.createdAt);
order_idrestaurant_idtotalprev_totalchange
1011700NULLNULL
1041400700-300
1051250400-150
1022360NULLNULL

Order 102 is NULL because it is the first row in its own pile (Dosa Den). Order 101 came earlier in time, but it belongs to Spice Hub.

Why this, not that

  • PARTITION BY restaurant_id vs restaurant_id in ORDER BY: with the group only in ORDER BY, nothing restarts. This wrong query, lag(total) OVER (ORDER BY restaurant_id, created_at), gives:
order_idrestaurant_idtotalprev_total (wrong)
1011700NULL
1041400700
1051250400
1022360250

Order 102 now compares with Spice Hub’s last order (250). Put the group in PARTITION BY.

  • Window vs GROUP BY: GROUP BY leaves one row per restaurant, so no order row is left to compare with.
  • Default value, one line: lag(total, 1, 0) returns 0 instead of NULL when there is no previous row.

Trigger card

When I see “previous”, “change since last” or “compared to last time”, I use lag(x) OVER (PARTITION BY group ORDER BY time), because LAG reads the row before mine and PARTITION BY restarts it for each group.

Say it in an interview

“A window function computes a value for each row from related rows, and keeps every row, unlike GROUP BY. OVER defines the window. PARTITION BY splits the rows into groups that restart, and ORDER BY inside OVER sets the order, for ranks, running totals and LAG. Window functions run after WHERE, GROUP BY and HAVING, so you can’t filter on one in WHERE. For top N per group, I rank in a CTE with rank() OVER (PARTITION BY group ORDER BY value DESC) and filter the rank outside. For a running total I group by the day first, then sum() OVER (ORDER BY day). For the previous value I use LAG with PARTITION BY, and the first row of each group is NULL.”

Questions interviewers ask

Mental map

Window functions mental map

Watch

Alex The Analyst, about 4 minutes: “Intermediate SQL Tutorial | Partition By”. It covers PARTITION BY, the per-group part of OVER. It does not cover rank, running totals or LAG, so use the sections above for those.

Want more? The detailed Window functions page has the same sections with every step table, the wrong version’s table, the default frame in full, ROWS vs RANGE, LAST_VALUE, and more interview questions.


← Aggregates: GROUP BY and HAVING · Back to Databases and SQL →