SQL Window Functions
Calculate rankings and running metrics without collapsing detail rows.
How it works
Window functions perform calculations across related rows while keeping each original row visible. That makes them ideal for rankings, running totals and previous/next-row comparisons.
PARTITION BY restarts the calculation for each group. ORDER BY inside OVER defines the sequence used by the window function.
Rank the latest transaction per PO, find the largest usage per material or calculate running production totals by shift.
The pattern
ROW_NUMBER() OVER (
PARTITION BY group_column
ORDER BY value DESC
)Example
SELECT OrderID, CustomerID, Quantity,
ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY Quantity DESC) AS rn
FROM Orders;Run the example
Edit the query and run it against the built-in demo tables. Nothing is sent to a server.
The learning runner supports SELECT, WHERE, LIKE, IN, BETWEEN, GROUP BY, HAVING, JOIN, aggregates, ORDER BY and LIMIT. Advanced lessons explain additional production SQL concepts even when the mini runner does not execute that syntax.
Does GROUP BY keep every original detail row?
Progress is stored only in this browser. No account required.